From de85113e730cd0a7592345d24721a8280d3c5822 Mon Sep 17 00:00:00 2001 From: liwei1dao Date: Sat, 14 Mar 2026 09:58:53 +0800 Subject: [PATCH] =?UTF-8?q?feat(audio):=20=E6=B7=BB=E5=8A=A0=E5=90=88?= =?UTF-8?q?=E6=88=90=E4=BB=BB=E5=8A=A1=E8=AE=A1=E6=95=B0=E4=BB=A5=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E6=92=AD=E6=94=BE=E5=AE=8C=E6=88=90=E4=BA=8B=E4=BB=B6?= =?UTF-8?q?=E8=A7=A6=E5=8F=91=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../azure_speech/AzureTtsHelper.kt | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/local_plugins/azure_speech/android/src/main/kotlin/com/yunqiinnovation/azure_speech/AzureTtsHelper.kt b/local_plugins/azure_speech/android/src/main/kotlin/com/yunqiinnovation/azure_speech/AzureTtsHelper.kt index 05223bd8d..5b0770249 100644 --- a/local_plugins/azure_speech/android/src/main/kotlin/com/yunqiinnovation/azure_speech/AzureTtsHelper.kt +++ b/local_plugins/azure_speech/android/src/main/kotlin/com/yunqiinnovation/azure_speech/AzureTtsHelper.kt @@ -14,6 +14,7 @@ import com.deep_voice.speech.tts.TtsEventType import kotlinx.coroutines.* import java.io.ByteArrayInputStream import java.io.InputStream +import java.util.concurrent.atomic.AtomicInteger import com.yunqiinnovation.azure_speech.tools.SimpleAudioPlayer /** * Azure TTS Helper @@ -51,6 +52,9 @@ class AzureTtsHelper(private val context: Context) : ITtsService { private val streamBuffer = StringBuilder() private var lastSpeakTime = 0L + // 待完成的合成任务计数:只有全部完成才触发 PLAYBACK_COMPLETED + private val pendingSpeakCount = AtomicInteger(0) + private var enableVoiceFlocking = false private var speakerProfileId: String = "" @@ -310,8 +314,11 @@ class AzureTtsHelper(private val context: Context) : ITtsService { ) isSpeaking = false notifyEvent(TtsEventType.SYNTHESIS_COMPLETED) - // Azure TTS 合成完成即播放完成 - notifyEvent(TtsEventType.PLAYBACK_COMPLETED) + // 所有句子都播完后才触发 PLAYBACK_COMPLETED,避免多句流式合成时重复触发 + if (pendingSpeakCount.decrementAndGet() <= 0) { + pendingSpeakCount.set(0) + notifyEvent(TtsEventType.PLAYBACK_COMPLETED) + } } // 合成取消事件 @@ -561,8 +568,9 @@ class AzureTtsHelper(private val context: Context) : ITtsService { val ssml = generateOptimizedSsml(text) isSpeaking = true + pendingSpeakCount.incrementAndGet() - FileLogger.d(TAG, "开始语音合成,文本长度: ${text.length}") + FileLogger.d(TAG, "开始语音合成,文本长度: ${text.length},当前待完成任务: ${pendingSpeakCount.get()}") // 直接调用SpeakSsmlAsync,SDK内部已经是异步的 synthesizer?.SpeakSsmlAsync(ssml) @@ -700,6 +708,7 @@ class AzureTtsHelper(private val context: Context) : ITtsService { synthesizer?.StopSpeakingAsync() streamBuffer.clear() isSpeaking = false + pendingSpeakCount.set(0) recreateSynthesizer() return true } catch (e: Exception) {