From c3bfbfe0e51e0b0e3ce8fb5d919beca7e82376fa Mon Sep 17 00:00:00 2001 From: tanlongsheng <252620078@qq.com> Date: Mon, 8 Dec 2025 20:19:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=AE=AF=E9=A3=9E=E8=AF=AD?= =?UTF-8?q?=E9=9F=B3=E8=AF=86=E5=88=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../azure_speech/AzureAsrHelper.kt | 6 ++++++ .../azure_speech/XunFeiAsrHelper.kt | 18 ++++++++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/local_plugins/azure_speech/android/src/main/kotlin/com/yunqiinnovation/azure_speech/AzureAsrHelper.kt b/local_plugins/azure_speech/android/src/main/kotlin/com/yunqiinnovation/azure_speech/AzureAsrHelper.kt index ed74769ee..3f6aaed83 100644 --- a/local_plugins/azure_speech/android/src/main/kotlin/com/yunqiinnovation/azure_speech/AzureAsrHelper.kt +++ b/local_plugins/azure_speech/android/src/main/kotlin/com/yunqiinnovation/azure_speech/AzureAsrHelper.kt @@ -289,6 +289,12 @@ class AzureAsrHelper(private val context: Context) { this.audioSourceType = audioSourceType if (!audioStream.isContinuousRecognitionActive) { + // 确保音频流已初始化 + if (!audioStream.isInitialized()) { + audioStream.initAudioRecord() + audioStream.setupStream() + } + audioStream.startAudioRecord( when (audioSourceType) { AudioSourceType.MICROPHONE -> SimpleAudioReceiver.AudioSourceType.MICROPHONE diff --git a/local_plugins/azure_speech/android/src/main/kotlin/com/yunqiinnovation/azure_speech/XunFeiAsrHelper.kt b/local_plugins/azure_speech/android/src/main/kotlin/com/yunqiinnovation/azure_speech/XunFeiAsrHelper.kt index 18da0e3ae..3c4253dd0 100644 --- a/local_plugins/azure_speech/android/src/main/kotlin/com/yunqiinnovation/azure_speech/XunFeiAsrHelper.kt +++ b/local_plugins/azure_speech/android/src/main/kotlin/com/yunqiinnovation/azure_speech/XunFeiAsrHelper.kt @@ -25,6 +25,13 @@ class XunFeiAsrHelper(private val context: Context) { private var currentSessionId = "" fun start(callback: AzureAsrHelper.ContinuousRecognizeCallback) { + try { + webSocket?.cancel() + webSocket = null + } catch (e: Exception) { + Log.e(tag, "Clear previous websocket failed: ${e.message}") + } + currentSessionId = UUID.randomUUID().toString() val url = getWebSocketUrl() @@ -64,8 +71,15 @@ class XunFeiAsrHelper(private val context: Context) { } fun stop() { - webSocket?.let { ws: WebSocket -> - ws.send("{\"end\": true}") + try { + webSocket?.let { ws: WebSocket -> + ws.send("{\"end\": true}") + ws.close(1000, "User stopped") + } + } catch (e: Exception) { + Log.e(tag, "Stop Xunfei failed: ${e.message}") + } finally { + webSocket = null } }