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.
This commit is contained in:
1 parent
2edd9bcc06
commit
0afc74e567
1 file changed
+8
-4
@@ -185,6 +185,7 @@ class AudioRecorderManager {
|
||||
@Volatile var elapsedMs: Int = 0; private set
|
||||
var pausedElapsedMs: Int = 0; private set
|
||||
private var playThread: Thread? = null
|
||||
@Volatile private var stopped = false
|
||||
|
||||
fun playVoiceFile(filePath: String, onProgress: ((Float) -> Unit)? = null, onComplete: () -> Unit = {}) {
|
||||
playFromPosition(filePath, 0, onProgress, onComplete)
|
||||
@@ -245,19 +246,22 @@ class AudioRecorderManager {
|
||||
} catch (e: Exception) {
|
||||
LogManager.addLog("ERROR", "AudioPlayer", "play error: ${e.message}")
|
||||
}
|
||||
playing = false; isPaused = false
|
||||
onComplete()
|
||||
if (!stopped) {
|
||||
playing = false; isPaused = false; onComplete()
|
||||
}
|
||||
}.apply { start() }
|
||||
}
|
||||
|
||||
fun pausePlayback() {
|
||||
pausedElapsedMs = elapsedMs
|
||||
isPaused = true; playing = false
|
||||
stopped = true; isPaused = true; playing = false
|
||||
playThread?.join(500); playThread = null
|
||||
stopped = false
|
||||
}
|
||||
|
||||
fun stopPlayback() {
|
||||
playing = false; isPaused = false; pausedElapsedMs = 0
|
||||
stopped = true; playing = false; isPaused = false; pausedElapsedMs = 0
|
||||
playThread?.join(500); playThread = null
|
||||
stopped = false
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user