diff --git a/lib/modules/agent/controllers/agent_controller.dart b/lib/modules/agent/controllers/agent_controller.dart index 08bdffa81..22eaa937b 100644 --- a/lib/modules/agent/controllers/agent_controller.dart +++ b/lib/modules/agent/controllers/agent_controller.dart @@ -152,6 +152,48 @@ class AgentController extends GetxController { _initMusicStatus(); //初始化音乐状态 _setupMediaSessionCallbacks(); //设置媒体会话回调 _setupTextInputListener(); //设置文本输入监听器 + + try { + // 1. 使用 GetX 获取 APP 当前语言环境 (Locale),而不是设备语言 + final appLocale = Get.locale; + List languagesToSupport = []; + + if (appLocale != null) { + final lang = appLocale.languageCode; // e.g., 'en' + final country = appLocale.countryCode; // e.g., 'US' + + // 2. 构造 Azure SDK 需要的格式 (e.g., 'en-US') + if (country != null && country.isNotEmpty) { + languagesToSupport.add('$lang-$country'); + } else { + languagesToSupport.add(lang); // fallback only language code + } + } + + // 3. 添加 en-US 作为备选 + if (!languagesToSupport.contains('en-US')) { + languagesToSupport.add('en-US'); + } + + // 4. 最终兜底 + if (languagesToSupport.isEmpty) { + languagesToSupport.add('en-US'); + } + + // 5. 调用原生设置 + Logger.i(TAG, '支持的语言: ${languagesToSupport.join(', ')}'); + AgentService.setSupportedLanguages(languagesToSupport).then((success) { + if (success) { + Logger.i(TAG, '支持的语言已根据 APP 设置语言: ${languagesToSupport.join(', ')}'); + } else { + Logger.w(TAG, '根据 APP 设置语言失败'); + } + }); + } catch (e) { + Logger.e(TAG, "获取 APP 语言并设置失败: $e"); + AgentService.setSupportedLanguages(['zh-CN', 'en-US']); + } + startVoiceInput(); //开启语音输入 } diff --git a/local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentService.kt b/local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentService.kt index 37ebe489e..1437d4982 100644 --- a/local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentService.kt +++ b/local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentService.kt @@ -52,6 +52,9 @@ interface AgentServiceListener { object AgentService : CoroutineScope { private val TAG = "AgentService" + private var azureSpeechKey: String = "" + private var azureSpeechRegion: String = "" + // 协程相关 private val job = SupervisorJob() override val coroutineContext: CoroutineContext @@ -179,6 +182,9 @@ object AgentService : CoroutineScope { LocationService.initialize(appContext) // 设置系统提示词 val customSystemPrompt = config["systemPrompt"] as? String + //保存配置 + this.azureSpeechKey = config["azureSpeechKey"]?.toString() ?: "" + this.azureSpeechRegion = config["azureSpeechRegion"]?.toString() ?: "" if (!customSystemPrompt.isNullOrBlank()) { systemPrompt = customSystemPrompt } else { @@ -188,9 +194,15 @@ object AgentService : CoroutineScope { azureAsrHelper = AzureAsrHelper(context).apply { // 使用协程处理异步初始化 CoroutineScope(Dispatchers.IO).launch { + //获取语音识别的语种 + val supportedLanguagesConfig = config["supportedLanguages"] as? List + val supportedLanguages = supportedLanguagesConfig?.toTypedArray() + ?: arrayOf("zh-CN", "en-US") // 默认支持多语言 + val asrInitSuccess = initialize( subscriptionKey = config["azureSpeechKey"]?.toString() ?: "", region = config["azureSpeechRegion"]?.toString() ?: "", + supportedLanguages = supportedLanguages, audioSourceType = if (isExternalActive) AzureAsrHelper.AudioSourceType.EXTERNAL else AzureAsrHelper.AudioSourceType.MICROPHONE ) @@ -1091,6 +1103,58 @@ object AgentService : CoroutineScope { } } + /** + * 设置支持的语音识别语言 + */ + fun setSupportedLanguages(languages: List): Boolean { + if (!isInitialized) { + Log.e(TAG, "服务未初始化") + sendEvent("error", mapOf("code" to "NOT_INITIALIZED", "message" to "服务未初始化")) + return false + } + + if (languages.isEmpty()) { + Log.e(TAG, "语言列表不能为空") + sendEvent("error", mapOf("code" to "EMPTY_LANGUAGES", "message" to "语言列表不能为空")) + return false + } + + return try { + Log.i(TAG, "设置支持的语言: ${languages.joinToString(", ")}") + + // 重新初始化ASR服务以应用新的语言设置 + val success = azureAsrHelper?.initialize( + subscriptionKey = this.azureSpeechKey, + region = this.azureSpeechRegion, + supportedLanguages = languages.toTypedArray(), + audioSourceType = audioSourceType + ) ?: false + + if (success) { + Log.i(TAG, "语言设置更新成功") + sendEvent("languages_updated", mapOf( + "languages" to languages, + "status" to "success" + )) + } else { + Log.e(TAG, "语言设置更新失败") + sendEvent("error", mapOf( + "code" to "LANGUAGE_UPDATE_FAILED", + "message" to "语言设置更新失败" + )) + } + + success + } catch (e: Exception) { + Log.e(TAG, "设置支持语言异常: ${e.message}") + sendEvent("error", mapOf( + "code" to "LANGUAGE_UPDATE_ERROR", + "message" to e.message.toString() + )) + false + } + } + /** * 保存聊天消息 */ diff --git a/local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentServicePlugin.kt b/local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentServicePlugin.kt index 1a5d06561..8229f4863 100644 --- a/local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentServicePlugin.kt +++ b/local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentServicePlugin.kt @@ -201,6 +201,22 @@ class AgentServicePlugin : FlutterPlugin, MethodCallHandler, EventChannel.Stream } } + "setSupportedLanguages" -> { + val languages = call.argument>("languages") + if (languages.isNullOrEmpty()) { + result.error("INVALID_ARGUMENTS", "语言列表不能为空", null) + return + } + + try { + val isSuccess = AgentService.setSupportedLanguages(languages) + result.success(isSuccess) + } catch (e: Exception) { + Log.e(TAG, "设置支持语言失败", e) + result.error("SET_LANGUAGES_ERROR", "设置支持语言失败: ${e.message}", null) + } + } + "stopAiSteam" -> { try { AgentService.stopAiStream() diff --git a/local_plugins/agent_service/ios/agent_service/Sources/agent_service/AgentServiceImpl.swift b/local_plugins/agent_service/ios/agent_service/Sources/agent_service/AgentServiceImpl.swift index 0dc9119f6..b817d7713 100644 --- a/local_plugins/agent_service/ios/agent_service/Sources/agent_service/AgentServiceImpl.swift +++ b/local_plugins/agent_service/ios/agent_service/Sources/agent_service/AgentServiceImpl.swift @@ -196,6 +196,9 @@ class AgentServiceImpl: NSObject { private func initializeAzureSpeech() -> Bool { azureTtsHelper?.addListener(self) + + // 获取支持的语言列表,支持从配置中读取 + let supportedLanguages = config["supportedLanguages"] as? [String] ?? ["zh-CN", "en-US"] guard let asrSuccess = azureAsrHelper?.initialize( subscriptionKey: azureSpeechKey, diff --git a/local_plugins/agent_service/lib/agent_service.dart b/local_plugins/agent_service/lib/agent_service.dart index 664b3faad..ede6b56dc 100644 --- a/local_plugins/agent_service/lib/agent_service.dart +++ b/local_plugins/agent_service/lib/agent_service.dart @@ -332,6 +332,21 @@ class AgentService { } } + /// 设置支持的语音识别语言 + /// + /// [languages] 支持的语言列表,如 ['zh-CN', 'en-US'] + /// 返回是否设置成功 + static Future setSupportedLanguages(List languages) async { + try { + final bool result = await _channel.invokeMethod('setSupportedLanguages', { + 'languages': languages, + }); + return result; + } on PlatformException catch (e) { + throw AgentServiceException(e.code, e.message ?? '设置支持语言失败', e.details); + } + } + /// 停止语音合成 static Future stopAiSteam() async { try { 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 ce143a45c..17e073b0a 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 @@ -87,7 +87,7 @@ class AzureAsrHelper(private val context: Context) { fun initialize( subscriptionKey: String, region: String, - supportedLanguages: Array = arrayOf("zh-CN"), + supportedLanguages: Array = arrayOf("zh-CN", "en-US"), audioSourceType: AudioSourceType = AudioSourceType.MICROPHONE ): Boolean { try {