From 39760e4c35c94d30dea68bfb5d24cddc8cbef12a Mon Sep 17 00:00:00 2001 From: lxm <1274844347@qq.com> Date: Mon, 3 Nov 2025 21:21:36 +0800 Subject: [PATCH] =?UTF-8?q?fix(speech):=20=E4=BF=AE=E5=A4=8D=E8=AF=AD?= =?UTF-8?q?=E9=9F=B3=E6=9C=8D=E5=8A=A1=E8=B5=84=E6=BA=90=E9=87=8A=E6=94=BE?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 确保语音服务在关闭时正确释放底层资源 移除翻译控制器中不必要的服务销毁调用以避免性能问题 --- .../speech_impl/azure_asr_service.dart | 7 ++++++ .../speech_impl/azure_ast_service.dart | 24 +++++++++++++++---- .../controllers/translation_controller.dart | 11 +++++---- 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/lib/data/services/speech_impl/azure_asr_service.dart b/lib/data/services/speech_impl/azure_asr_service.dart index eb003736a..12e43489c 100644 --- a/lib/data/services/speech_impl/azure_asr_service.dart +++ b/lib/data/services/speech_impl/azure_asr_service.dart @@ -139,6 +139,13 @@ class AzureAsrService extends GetxService implements AsrService { } } + @override + void onClose() async { + // 页面或服务被删除时,确保释放底层资源 + await dispose(); + super.onClose(); + } + @override Future recognizeOnce() async { if (!_isInitialized) { diff --git a/lib/data/services/speech_impl/azure_ast_service.dart b/lib/data/services/speech_impl/azure_ast_service.dart index 5b5dc35da..97cb98335 100644 --- a/lib/data/services/speech_impl/azure_ast_service.dart +++ b/lib/data/services/speech_impl/azure_ast_service.dart @@ -54,6 +54,13 @@ class AzureAstService extends GetxService implements AstService { _loadConfig(); } + @override + void onClose() async { + // 页面或服务被删除时,确保释放底层资源 + await dispose(); + super.onClose(); + } + /// 设置事件通道 void _setupEventChannel() { _eventSubscription?.cancel(); @@ -244,12 +251,19 @@ class AzureAstService extends GetxService implements AstService { @override Future dispose() async { try { - final bool result = await _channel.invokeMethod('dispose'); - - return; + // 取消事件订阅,关闭事件流,重置状态(补充释放,避免监听残留) + await _eventSubscription?.cancel(); + _eventSubscription = null; + await _eventStreamController?.close(); + _eventStreamController = null; + + // 通知原生端释放资源,并重置初始化标记 + await _channel.invokeMethod('dispose'); + _isInitialized = false; + Logger.info('Azure AST 资源已释放'); } catch (e) { - Logger.error('停止录音: ${e.toString()}'); - rethrow; + Logger.error('Azure AST 资源释放失败: ${e.toString()}'); + _isInitialized = false; } } diff --git a/lib/modules/translation/controllers/translation_controller.dart b/lib/modules/translation/controllers/translation_controller.dart index 8c63c903d..dcb70b5ba 100644 --- a/lib/modules/translation/controllers/translation_controller.dart +++ b/lib/modules/translation/controllers/translation_controller.dart @@ -234,7 +234,7 @@ class TranslationController extends GetxController with WidgetsBindingObserver { Future changeTranslationMode(String mode) async { currentMode.value = mode; currentModeTitle.value = _getModeTitle(mode); - stopAll(); + //stopAll(); // 检查悬浮窗支持情况 if (isFloatingWindowEnabled.value && !_isFloatingWindowSupportedMode()) { @@ -592,7 +592,7 @@ class TranslationController extends GetxController with WidgetsBindingObserver { sourceLanguageCode.value, targetLanguageCode.value ]; - Logger.info('2初始化ASR服务,支持语言: $asrSupportedLanguages'); + //Logger.info('2初始化ASR服务,支持语言: $asrSupportedLanguages'); //await _asrService.initialize(supportedLanguages: asrSupportedLanguages); @@ -640,7 +640,7 @@ class TranslationController extends GetxController with WidgetsBindingObserver { sourceLanguageCode.value, targetLanguageCode.value ]; - + Logger.info('1初始化AST服务,支持语言: $callModeLanguages'); //ast=asr+translate await _astService.initialize(supportedLanguages: callModeLanguages); Logger.info('通话模式语音翻译服务初始化完成'); } catch (e) { @@ -656,8 +656,9 @@ class TranslationController extends GetxController with WidgetsBindingObserver { _voiceInteractionSubscription?.cancel(); _translationDebounceTimer?.cancel(); restoreOtherServices(); - _asrService.dispose(); - _astService.dispose(); + // 移除对服务的 dispose,避免频繁销毁/重建造成卡顿 + // _asrService.dispose(); + // _astService.dispose(); // 释放管理器资源 _timerManager.dispose();