Browse Source

feat(语音识别): 添加agent模块多语言支持配置功能

实现语音识别多语言动态配置,支持根据APP语言环境自动设置
新增setSupportedLanguages方法用于设置支持的语言列表
默认添加en-US作为备选语言
weicu
lxm 1 year ago
parent
commit
0e11fde673
  1. 42
      lib/modules/agent/controllers/agent_controller.dart
  2. 64
      local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentService.kt
  3. 16
      local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentServicePlugin.kt
  4. 3
      local_plugins/agent_service/ios/agent_service/Sources/agent_service/AgentServiceImpl.swift
  5. 15
      local_plugins/agent_service/lib/agent_service.dart
  6. 2
      local_plugins/azure_speech/android/src/main/kotlin/com/yunqiinnovation/azure_speech/AzureAsrHelper.kt

42
lib/modules/agent/controllers/agent_controller.dart

@ -152,6 +152,48 @@ class AgentController extends GetxController {
_initMusicStatus(); //初始化音乐状态 _initMusicStatus(); //初始化音乐状态
_setupMediaSessionCallbacks(); //设置媒体会话回调 _setupMediaSessionCallbacks(); //设置媒体会话回调
_setupTextInputListener(); //设置文本输入监听器 _setupTextInputListener(); //设置文本输入监听器
try {
// 1. 使用 GetX 获取 APP 当前语言环境 (Locale),而不是设备语言
final appLocale = Get.locale;
List<String> 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(); //开启语音输入 startVoiceInput(); //开启语音输入
} }

64
local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentService.kt

@ -52,6 +52,9 @@ interface AgentServiceListener {
object AgentService : CoroutineScope { object AgentService : CoroutineScope {
private val TAG = "AgentService" private val TAG = "AgentService"
private var azureSpeechKey: String = ""
private var azureSpeechRegion: String = ""
// 协程相关 // 协程相关
private val job = SupervisorJob() private val job = SupervisorJob()
override val coroutineContext: CoroutineContext override val coroutineContext: CoroutineContext
@ -179,6 +182,9 @@ object AgentService : CoroutineScope {
LocationService.initialize(appContext) LocationService.initialize(appContext)
// 设置系统提示词 // 设置系统提示词
val customSystemPrompt = config["systemPrompt"] as? String val customSystemPrompt = config["systemPrompt"] as? String
//保存配置
this.azureSpeechKey = config["azureSpeechKey"]?.toString() ?: ""
this.azureSpeechRegion = config["azureSpeechRegion"]?.toString() ?: ""
if (!customSystemPrompt.isNullOrBlank()) { if (!customSystemPrompt.isNullOrBlank()) {
systemPrompt = customSystemPrompt systemPrompt = customSystemPrompt
} else { } else {
@ -188,9 +194,15 @@ object AgentService : CoroutineScope {
azureAsrHelper = AzureAsrHelper(context).apply { azureAsrHelper = AzureAsrHelper(context).apply {
// 使用协程处理异步初始化 // 使用协程处理异步初始化
CoroutineScope(Dispatchers.IO).launch { CoroutineScope(Dispatchers.IO).launch {
//获取语音识别的语种
val supportedLanguagesConfig = config["supportedLanguages"] as? List<String>
val supportedLanguages = supportedLanguagesConfig?.toTypedArray()
?: arrayOf("zh-CN", "en-US") // 默认支持多语言
val asrInitSuccess = initialize( val asrInitSuccess = initialize(
subscriptionKey = config["azureSpeechKey"]?.toString() ?: "", subscriptionKey = config["azureSpeechKey"]?.toString() ?: "",
region = config["azureSpeechRegion"]?.toString() ?: "", region = config["azureSpeechRegion"]?.toString() ?: "",
supportedLanguages = supportedLanguages,
audioSourceType = if (isExternalActive) AzureAsrHelper.AudioSourceType.EXTERNAL audioSourceType = if (isExternalActive) AzureAsrHelper.AudioSourceType.EXTERNAL
else AzureAsrHelper.AudioSourceType.MICROPHONE else AzureAsrHelper.AudioSourceType.MICROPHONE
) )
@ -1091,6 +1103,58 @@ object AgentService : CoroutineScope {
} }
} }
/**
* 设置支持的语音识别语言
*/
fun setSupportedLanguages(languages: List<String>): 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
}
}
/** /**
* 保存聊天消息 * 保存聊天消息
*/ */

16
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<List<String>>("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" -> { "stopAiSteam" -> {
try { try {
AgentService.stopAiStream() AgentService.stopAiStream()

3
local_plugins/agent_service/ios/agent_service/Sources/agent_service/AgentServiceImpl.swift

@ -196,6 +196,9 @@ class AgentServiceImpl: NSObject {
private func initializeAzureSpeech() -> Bool { private func initializeAzureSpeech() -> Bool {
azureTtsHelper?.addListener(self) azureTtsHelper?.addListener(self)
// 获取支持的语言列表,支持从配置中读取
let supportedLanguages = config["supportedLanguages"] as? [String] ?? ["zh-CN", "en-US"]
guard let asrSuccess = azureAsrHelper?.initialize( guard let asrSuccess = azureAsrHelper?.initialize(
subscriptionKey: azureSpeechKey, subscriptionKey: azureSpeechKey,

15
local_plugins/agent_service/lib/agent_service.dart

@ -332,6 +332,21 @@ class AgentService {
} }
} }
/// 设置支持的语音识别语言
///
/// [languages] 支持的语言列表,如 ['zh-CN', 'en-US']
/// 返回是否设置成功
static Future<bool> setSupportedLanguages(List<String> 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<bool> stopAiSteam() async { static Future<bool> stopAiSteam() async {
try { try {

2
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( fun initialize(
subscriptionKey: String, subscriptionKey: String,
region: String, region: String,
supportedLanguages: Array<String> = arrayOf("zh-CN"), supportedLanguages: Array<String> = arrayOf("zh-CN", "en-US"),
audioSourceType: AudioSourceType = AudioSourceType.MICROPHONE audioSourceType: AudioSourceType = AudioSourceType.MICROPHONE
): Boolean { ): Boolean {
try { try {

Loading…
Cancel
Save