Browse Source

fix: stop/pause no longer fires onComplete from killed thread

When stopPlayback/pausePlayback kills the playback thread, the thread's
onComplete callback was still firing, resetting UI state (playing=false,
elapsed=0). Added @Volatile 'stopped' flag — onComplete only fires on
natural completion, not on external stop/pause.
topo_upd
evgeny 2 months ago
parent
commit
0afc74e567
  1. 12
      tools/chatgui-android/app/src/main/java/com/utun/chat/data/AudioRecorderManager.kt

12
tools/chatgui-android/app/src/main/java/com/utun/chat/data/AudioRecorderManager.kt

@ -185,6 +185,7 @@ class AudioRecorderManager {
@Volatile var elapsedMs: Int = 0; private set @Volatile var elapsedMs: Int = 0; private set
var pausedElapsedMs: Int = 0; private set var pausedElapsedMs: Int = 0; private set
private var playThread: Thread? = null private var playThread: Thread? = null
@Volatile private var stopped = false
fun playVoiceFile(filePath: String, onProgress: ((Float) -> Unit)? = null, onComplete: () -> Unit = {}) { fun playVoiceFile(filePath: String, onProgress: ((Float) -> Unit)? = null, onComplete: () -> Unit = {}) {
playFromPosition(filePath, 0, onProgress, onComplete) playFromPosition(filePath, 0, onProgress, onComplete)
@ -245,19 +246,22 @@ class AudioRecorderManager {
} catch (e: Exception) { } catch (e: Exception) {
LogManager.addLog("ERROR", "AudioPlayer", "play error: ${e.message}") LogManager.addLog("ERROR", "AudioPlayer", "play error: ${e.message}")
} }
playing = false; isPaused = false if (!stopped) {
onComplete() playing = false; isPaused = false; onComplete()
}
}.apply { start() } }.apply { start() }
} }
fun pausePlayback() { fun pausePlayback() {
pausedElapsedMs = elapsedMs pausedElapsedMs = elapsedMs
isPaused = true; playing = false stopped = true; isPaused = true; playing = false
playThread?.join(500); playThread = null playThread?.join(500); playThread = null
stopped = false
} }
fun stopPlayback() { fun stopPlayback() {
playing = false; isPaused = false; pausedElapsedMs = 0 stopped = true; playing = false; isPaused = false; pausedElapsedMs = 0
playThread?.join(500); playThread = null playThread?.join(500); playThread = null
stopped = false
} }
} }

Loading…
Cancel
Save