fix: 切换任务时取消正在进行的TTS合成请求
stop() 只停了 MediaPlayer,没取消 WebSocket 协程, 导致切换任务后上一个任务的语音下载完仍会播放。 现在 stop() 同时取消协程 Job。 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -80,6 +80,9 @@ class EdgeTtsManager @Inject constructor(
|
||||
/** 当前 MediaPlayer */
|
||||
private var mediaPlayer: MediaPlayer? = null
|
||||
|
||||
/** 当前合成协程 Job(用于取消正在进行的请求) */
|
||||
private var synthesizeJob: kotlinx.coroutines.Job? = null
|
||||
|
||||
/** 是否正在播放 */
|
||||
var isPlaying: Boolean = false
|
||||
private set
|
||||
@@ -99,10 +102,10 @@ class EdgeTtsManager @Inject constructor(
|
||||
fun speak(text: String, voice: String = VOICE_XIAOXIAO, onError: ((String) -> Unit)? = null) {
|
||||
if (text.isBlank()) return
|
||||
|
||||
// 停止当前播放
|
||||
// 停止当前播放和进行中的请求
|
||||
stop()
|
||||
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
synthesizeJob = CoroutineScope(Dispatchers.IO).launch {
|
||||
try {
|
||||
// 检查缓存
|
||||
val cacheFile = getCacheFile(text, voice)
|
||||
@@ -129,8 +132,12 @@ class EdgeTtsManager @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
/** 停止播放 */
|
||||
/** 停止播放和进行中的合成请求 */
|
||||
fun stop() {
|
||||
// 取消正在进行的合成协程(含 WebSocket 请求)
|
||||
synthesizeJob?.cancel()
|
||||
synthesizeJob = null
|
||||
// 停止音频播放
|
||||
try {
|
||||
mediaPlayer?.apply {
|
||||
if (isPlaying) stop()
|
||||
|
||||
Reference in New Issue
Block a user