|
|
@ -10,12 +10,17 @@ import android.util.Log |
|
|
import com.ryanheise.audioservice.AudioServiceActivity |
|
|
import com.ryanheise.audioservice.AudioServiceActivity |
|
|
import io.flutter.embedding.engine.FlutterEngine |
|
|
import io.flutter.embedding.engine.FlutterEngine |
|
|
import io.flutter.plugin.common.MethodChannel |
|
|
import io.flutter.plugin.common.MethodChannel |
|
|
|
|
|
import io.flutter.plugin.common.MethodCall |
|
|
import io.flutter.plugin.common.EventChannel |
|
|
import io.flutter.plugin.common.EventChannel |
|
|
import io.flutter.embedding.engine.plugins.FlutterPlugin |
|
|
import io.flutter.embedding.engine.plugins.FlutterPlugin |
|
|
import io.flutter.plugins.GeneratedPluginRegistrant |
|
|
import io.flutter.plugins.GeneratedPluginRegistrant |
|
|
import io.flutter.embedding.android.FlutterActivity |
|
|
import io.flutter.embedding.android.FlutterActivity |
|
|
import android.content.Context |
|
|
import android.content.Context |
|
|
import android.content.ComponentName |
|
|
import android.content.ComponentName |
|
|
|
|
|
import android.content.BroadcastReceiver |
|
|
|
|
|
import android.content.IntentFilter |
|
|
|
|
|
import android.os.Handler |
|
|
|
|
|
import android.os.Looper |
|
|
|
|
|
|
|
|
class MainActivity: FlutterActivity() { |
|
|
class MainActivity: FlutterActivity() { |
|
|
|
|
|
|
|
|
@ -24,18 +29,49 @@ class MainActivity: FlutterActivity() { |
|
|
private val AZURE_ASR_EVENT_CHANNEL = "com.deep_voice.azure_asr_events" |
|
|
private val AZURE_ASR_EVENT_CHANNEL = "com.deep_voice.azure_asr_events" |
|
|
private val AZURE_TTS_CHANNEL = "com.deep_voice.azure_tts" |
|
|
private val AZURE_TTS_CHANNEL = "com.deep_voice.azure_tts" |
|
|
private val VOICE_INTERACTION_CHANNEL = "com.deep_voice.voice_interaction" |
|
|
private val VOICE_INTERACTION_CHANNEL = "com.deep_voice.voice_interaction" |
|
|
|
|
|
private val VOICE_INTERACTION_EVENT_CHANNEL = "com.deep_voice.voice_interaction_events" |
|
|
private val TAG = "MainActivity" |
|
|
private val TAG = "MainActivity" |
|
|
private lateinit var azureAsrHelper: AzureAsrHelper |
|
|
private lateinit var azureAsrHelper: AzureAsrHelper |
|
|
private lateinit var azureTtsHelper: AzureTtsHelper |
|
|
private lateinit var azureTtsHelper: AzureTtsHelper |
|
|
private var azureAsrEventSink: EventChannel.EventSink? = null |
|
|
private var azureAsrEventSink: EventChannel.EventSink? = null |
|
|
|
|
|
private var voiceInteractionEventSink: EventChannel.EventSink? = null |
|
|
|
|
|
|
|
|
|
|
|
// 广播接收器 |
|
|
|
|
|
private val voiceInteractionReceiver = object : BroadcastReceiver() { |
|
|
|
|
|
override fun onReceive(context: Context?, intent: Intent?) { |
|
|
|
|
|
Log.d(TAG, "收到广播: ${intent?.action}") |
|
|
|
|
|
|
|
|
|
|
|
when (intent?.action) { |
|
|
|
|
|
VoiceInteractionService.ACTION_RECOGNITION_STARTED -> { |
|
|
|
|
|
val timestamp = intent.getLongExtra("timestamp", 0) |
|
|
|
|
|
Log.d(TAG, "收到语音识别启动广播: timestamp=$timestamp") |
|
|
|
|
|
sendVoiceInteractionEvent(mapOf( |
|
|
|
|
|
"type" to "recognitionStarted", |
|
|
|
|
|
"timestamp" to timestamp |
|
|
|
|
|
)) |
|
|
|
|
|
} |
|
|
|
|
|
VoiceInteractionService.ACTION_CHAT_HISTORY_UPDATED -> { |
|
|
|
|
|
val agentId = intent.getStringExtra("agentId") ?: "default" |
|
|
|
|
|
val userMessage = intent.getStringExtra("userMessage") ?: "" |
|
|
|
|
|
val assistantMessage = intent.getStringExtra("assistantMessage") ?: "" |
|
|
|
|
|
val timestamp = intent.getLongExtra("timestamp", System.currentTimeMillis()) |
|
|
|
|
|
|
|
|
|
|
|
Log.d(TAG, "收到聊天记录更新广播: agentId=$agentId, timestamp=$timestamp") |
|
|
|
|
|
Log.d(TAG, "用户消息: ${userMessage.take(50)}...") |
|
|
|
|
|
Log.d(TAG, "助手回复: ${assistantMessage.take(50)}...") |
|
|
|
|
|
|
|
|
|
|
|
sendChatHistoryEvent(agentId, userMessage, assistantMessage, timestamp) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// 使用伴生对象存储配置信息,使其在整个应用中可访问 |
|
|
// 使用伴生对象存储配置信息,使其在整个应用中可访问 |
|
|
companion object { |
|
|
companion object { |
|
|
// Azure 语音服务配置 |
|
|
// Azure 语音服务配置 |
|
|
var azureSpeechKey: String = "" |
|
|
var azureSpeechKey: String = "2K2VOHSuL8ZSEnbKgHSyTwo1ohj7C2gbxyVimsjpVUt6aETxVwlvJQQJ99BCACqBBLyXJ3w3AAAYACOGqCMj" |
|
|
var azureSpeechRegion: String = "" |
|
|
var azureSpeechRegion: String = "southeastasia" |
|
|
var volcanoAiApiKey: String = "" |
|
|
var volcanoAiApiKey: String = "0fd7c93b-b9bf-4b0b-bca6-e284491792bf" |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) { |
|
|
override fun onCreate(savedInstanceState: Bundle?) { |
|
|
@ -43,16 +79,126 @@ class MainActivity: FlutterActivity() { |
|
|
azureAsrHelper = AzureAsrHelper() |
|
|
azureAsrHelper = AzureAsrHelper() |
|
|
azureTtsHelper = AzureTtsHelper() |
|
|
azureTtsHelper = AzureTtsHelper() |
|
|
|
|
|
|
|
|
|
|
|
// 注册广播接收器 |
|
|
|
|
|
registerVoiceInteractionReceiver() |
|
|
|
|
|
|
|
|
// 不再自动启动服务,由用户通过 Flutter 界面手动启动 |
|
|
// 不再自动启动服务,由用户通过 Flutter 界面手动启动 |
|
|
Log.d(TAG, "MainActivity 已创建,但不会自动启动 VoiceInteractionService") |
|
|
Log.d(TAG, "MainActivity 已创建,但不会自动启动 VoiceInteractionService") |
|
|
|
|
|
|
|
|
|
|
|
// 确保 Flutter 引擎准备好后立即设置事件通道 |
|
|
|
|
|
flutterEngine?.let { engine -> |
|
|
|
|
|
Log.d(TAG, "Flutter 引擎已存在,立即设置事件通道") |
|
|
|
|
|
setupEventChannels(engine) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 注册语音交互服务的广播接收器 |
|
|
|
|
|
*/ |
|
|
|
|
|
private fun registerVoiceInteractionReceiver() { |
|
|
|
|
|
val intentFilter = IntentFilter().apply { |
|
|
|
|
|
addAction(VoiceInteractionService.ACTION_RECOGNITION_STARTED) |
|
|
|
|
|
addAction(VoiceInteractionService.ACTION_CHAT_HISTORY_UPDATED) |
|
|
|
|
|
} |
|
|
|
|
|
registerReceiver(voiceInteractionReceiver, intentFilter) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
override fun configureFlutterEngine(flutterEngine: FlutterEngine) { |
|
|
override fun configureFlutterEngine(flutterEngine: FlutterEngine) { |
|
|
super.configureFlutterEngine(flutterEngine) |
|
|
Log.d(TAG, "开始配置 Flutter 引擎") |
|
|
|
|
|
|
|
|
|
|
|
// 先设置事件通道,确保在插件注册之前就已准备好 |
|
|
|
|
|
Log.d(TAG, "在插件注册前设置事件通道") |
|
|
|
|
|
setupEventChannels(flutterEngine) |
|
|
|
|
|
|
|
|
// 注册所有插件 |
|
|
// 然后调用父类方法和注册插件 |
|
|
|
|
|
super.configureFlutterEngine(flutterEngine) |
|
|
GeneratedPluginRegistrant.registerWith(flutterEngine) |
|
|
GeneratedPluginRegistrant.registerWith(flutterEngine) |
|
|
|
|
|
|
|
|
|
|
|
// 设置方法通道 |
|
|
|
|
|
setupMethodChannels(flutterEngine) |
|
|
|
|
|
|
|
|
|
|
|
Log.d(TAG, "Flutter 引擎配置完成") |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 设置事件通道 |
|
|
|
|
|
*/ |
|
|
|
|
|
private fun setupEventChannels(flutterEngine: FlutterEngine) { |
|
|
|
|
|
Log.d(TAG, "开始设置事件通道") |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
// 语音交互事件通道 |
|
|
|
|
|
Log.d(TAG, "设置语音交互事件通道") |
|
|
|
|
|
EventChannel(flutterEngine.dartExecutor.binaryMessenger, VOICE_INTERACTION_EVENT_CHANNEL).setStreamHandler( |
|
|
|
|
|
object : EventChannel.StreamHandler { |
|
|
|
|
|
override fun onListen(arguments: Any?, events: EventChannel.EventSink?) { |
|
|
|
|
|
Log.d(TAG, "语音交互事件通道开始监听,arguments: $arguments") |
|
|
|
|
|
voiceInteractionEventSink = events |
|
|
|
|
|
|
|
|
|
|
|
// 通知 Flutter 端事件通道已准备好 |
|
|
|
|
|
try { |
|
|
|
|
|
events?.success(mapOf("type" to "channelReady")) |
|
|
|
|
|
Log.d(TAG, "已发送 channelReady 事件") |
|
|
|
|
|
|
|
|
|
|
|
// 测试发送一个聊天记录事件 |
|
|
|
|
|
Handler(Looper.getMainLooper()).postDelayed({ |
|
|
|
|
|
if (voiceInteractionEventSink != null) { |
|
|
|
|
|
Log.d(TAG, "发送测试聊天记录事件") |
|
|
|
|
|
sendChatHistoryEvent( |
|
|
|
|
|
"personal_assistant", |
|
|
|
|
|
"这是一条测试用户消息", |
|
|
|
|
|
"这是一条测试助手回复", |
|
|
|
|
|
System.currentTimeMillis() |
|
|
|
|
|
) |
|
|
|
|
|
} else { |
|
|
|
|
|
Log.e(TAG, "无法发送测试事件:事件通道未准备好") |
|
|
|
|
|
} |
|
|
|
|
|
}, 5000) // 5秒后发送测试事件 |
|
|
|
|
|
} catch (e: Exception) { |
|
|
|
|
|
Log.e(TAG, "发送 channelReady 事件失败: ${e.message}", e) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
override fun onCancel(arguments: Any?) { |
|
|
|
|
|
Log.d(TAG, "语音交互事件通道取消监听,arguments: $arguments") |
|
|
|
|
|
voiceInteractionEventSink = null |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
) |
|
|
|
|
|
Log.d(TAG, "语音交互事件通道设置完成") |
|
|
|
|
|
|
|
|
|
|
|
// Azure ASR 事件通道 |
|
|
|
|
|
Log.d(TAG, "设置 Azure ASR 事件通道") |
|
|
|
|
|
EventChannel(flutterEngine.dartExecutor.binaryMessenger, AZURE_ASR_EVENT_CHANNEL).setStreamHandler( |
|
|
|
|
|
object : EventChannel.StreamHandler { |
|
|
|
|
|
override fun onListen(arguments: Any?, events: EventChannel.EventSink?) { |
|
|
|
|
|
Log.d(TAG, "ASR事件通道开始监听,arguments: $arguments") |
|
|
|
|
|
azureAsrEventSink = events |
|
|
|
|
|
|
|
|
|
|
|
// 通知 Flutter 端事件通道已准备好 |
|
|
|
|
|
events?.success(mapOf("type" to "channelReady")) |
|
|
|
|
|
Log.d(TAG, "已发送 ASR channelReady 事件") |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
override fun onCancel(arguments: Any?) { |
|
|
|
|
|
Log.d(TAG, "ASR事件通道取消监听,arguments: $arguments") |
|
|
|
|
|
azureAsrEventSink = null |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
) |
|
|
|
|
|
Log.d(TAG, "Azure ASR 事件通道设置完成") |
|
|
|
|
|
|
|
|
|
|
|
} catch (e: Exception) { |
|
|
|
|
|
Log.e(TAG, "设置事件通道时发生错误: ${e.message}", e) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Log.d(TAG, "所有事件通道设置完成") |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 设置方法通道 |
|
|
|
|
|
*/ |
|
|
|
|
|
private fun setupMethodChannels(flutterEngine: FlutterEngine) { |
|
|
// 音频处理通道 |
|
|
// 音频处理通道 |
|
|
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, AUDIO_PROCESSING_CHANNEL).setMethodCallHandler { call, result -> |
|
|
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, AUDIO_PROCESSING_CHANNEL).setMethodCallHandler { call, result -> |
|
|
// 为所有方法返回默认值或成功状态 |
|
|
// 为所有方法返回默认值或成功状态 |
|
|
@ -195,24 +341,6 @@ class MainActivity: FlutterActivity() { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Azure ASR 事件通道 |
|
|
|
|
|
EventChannel(flutterEngine.dartExecutor.binaryMessenger, AZURE_ASR_EVENT_CHANNEL).setStreamHandler( |
|
|
|
|
|
object : EventChannel.StreamHandler { |
|
|
|
|
|
override fun onListen(arguments: Any?, events: EventChannel.EventSink?) { |
|
|
|
|
|
Log.d(TAG, "ASR事件通道开始监听") |
|
|
|
|
|
azureAsrEventSink = events |
|
|
|
|
|
|
|
|
|
|
|
// 通知 Flutter 端事件通道已准备好 |
|
|
|
|
|
events?.success(mapOf("type" to "channelReady")) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
override fun onCancel(arguments: Any?) { |
|
|
|
|
|
Log.d(TAG, "ASR事件通道取消监听") |
|
|
|
|
|
azureAsrEventSink = null |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
// Azure TTS 方法通道 |
|
|
// Azure TTS 方法通道 |
|
|
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, AZURE_TTS_CHANNEL).setMethodCallHandler { call, result -> |
|
|
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, AZURE_TTS_CHANNEL).setMethodCallHandler { call, result -> |
|
|
when (call.method) { |
|
|
when (call.method) { |
|
|
@ -286,24 +414,7 @@ class MainActivity: FlutterActivity() { |
|
|
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, VOICE_INTERACTION_CHANNEL).setMethodCallHandler { call, result -> |
|
|
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, VOICE_INTERACTION_CHANNEL).setMethodCallHandler { call, result -> |
|
|
when (call.method) { |
|
|
when (call.method) { |
|
|
"startVoiceInteractionService" -> { |
|
|
"startVoiceInteractionService" -> { |
|
|
// 获取配置参数 |
|
|
startVoiceInteractionService(call, result) |
|
|
val azureSpeechKey = call.argument<String>("azure_speech_key") ?: "" |
|
|
|
|
|
val azureSpeechRegion = call.argument<String>("azure_speech_region") ?: "" |
|
|
|
|
|
val volcanoAiApiKey = call.argument<String>("volcano_ai_api_key") ?: "" |
|
|
|
|
|
|
|
|
|
|
|
// 保存配置到静态变量 |
|
|
|
|
|
MainActivity.azureSpeechKey = azureSpeechKey |
|
|
|
|
|
MainActivity.azureSpeechRegion = azureSpeechRegion |
|
|
|
|
|
MainActivity.volcanoAiApiKey = volcanoAiApiKey |
|
|
|
|
|
|
|
|
|
|
|
// 打印日志 |
|
|
|
|
|
Log.d(TAG, "已保存配置到静态变量:") |
|
|
|
|
|
Log.d(TAG, "azure_speech_key: ${if (azureSpeechKey.isNotEmpty()) azureSpeechKey.take(5) + "..." else "空"}") |
|
|
|
|
|
Log.d(TAG, "azure_speech_region: $azureSpeechRegion") |
|
|
|
|
|
Log.d(TAG, "volcano_ai_api_key: ${if (volcanoAiApiKey.isNotEmpty()) volcanoAiApiKey.take(5) + "..." else "空"}") |
|
|
|
|
|
|
|
|
|
|
|
startVoiceInteractionService() |
|
|
|
|
|
result.success(true) |
|
|
|
|
|
} |
|
|
} |
|
|
"stopVoiceInteractionService" -> { |
|
|
"stopVoiceInteractionService" -> { |
|
|
stopVoiceInteractionService() |
|
|
stopVoiceInteractionService() |
|
|
@ -312,6 +423,10 @@ class MainActivity: FlutterActivity() { |
|
|
"isVoiceInteractionServiceRunning" -> { |
|
|
"isVoiceInteractionServiceRunning" -> { |
|
|
result.success(VoiceInteractionService.isServiceRunning()) |
|
|
result.success(VoiceInteractionService.isServiceRunning()) |
|
|
} |
|
|
} |
|
|
|
|
|
"pauseVoiceInteraction" -> { |
|
|
|
|
|
pauseVoiceInteraction() |
|
|
|
|
|
result.success(true) |
|
|
|
|
|
} |
|
|
else -> { |
|
|
else -> { |
|
|
result.notImplemented() |
|
|
result.notImplemented() |
|
|
} |
|
|
} |
|
|
@ -347,35 +462,121 @@ class MainActivity: FlutterActivity() { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* 启动语音交互服务 |
|
|
* 启动语音交互服务(带配置参数) |
|
|
*/ |
|
|
*/ |
|
|
private fun startVoiceInteractionService() { |
|
|
private fun startVoiceInteractionService(call: MethodCall, result: MethodChannel.Result) { |
|
|
Log.d(TAG, "准备启动语音交互服务") |
|
|
Log.d(TAG, "启动语音交互服务(带配置参数)") |
|
|
val serviceIntent = Intent(this, VoiceInteractionService::class.java) |
|
|
|
|
|
|
|
|
|
|
|
// 添加 ACTION_MEDIA_BUTTON 动作,确保服务能够接收媒体按钮事件 |
|
|
// 获取配置参数 |
|
|
serviceIntent.action = Intent.ACTION_MEDIA_BUTTON |
|
|
val key = call.argument<String>("azure_speech_key") ?: "" |
|
|
|
|
|
val region = call.argument<String>("azure_speech_region") ?: "" |
|
|
|
|
|
val aiKey = call.argument<String>("volcano_ai_api_key") ?: "" |
|
|
|
|
|
|
|
|
// 确保明确设置组件 |
|
|
// 设置 Azure Speech 配置 |
|
|
serviceIntent.component = ComponentName(packageName, VoiceInteractionService::class.java.name) |
|
|
azureSpeechKey = key |
|
|
|
|
|
azureSpeechRegion = region |
|
|
|
|
|
volcanoAiApiKey = aiKey |
|
|
|
|
|
|
|
|
// 输出配置信息 |
|
|
Log.d(TAG, "配置参数: key=${if (key.isNotEmpty()) key.take(5) + "..." else "空"}, region=$region, aiKey=${if (aiKey.isNotEmpty()) aiKey.take(5) + "..." else "空"}") |
|
|
Log.d(TAG, "启动服务时的配置信息: subscriptionKey=${if (azureSpeechKey.isNotEmpty()) azureSpeechKey.take(5) + "..." else "空"}, serviceRegion=$azureSpeechRegion") |
|
|
|
|
|
|
|
|
|
|
|
Log.d(TAG, "启动服务: $serviceIntent") |
|
|
// 创建 Intent 并启动服务 |
|
|
startService(serviceIntent) |
|
|
val serviceIntent = Intent(this, VoiceInteractionService::class.java) |
|
|
Log.d(TAG, "语音交互服务启动完成") |
|
|
|
|
|
|
|
|
// 在 Android O 及以上版本,需要使用 startForegroundService |
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |
|
|
|
|
|
startForegroundService(serviceIntent) |
|
|
|
|
|
} else { |
|
|
|
|
|
startService(serviceIntent) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Log.d(TAG, "语音交互服务启动命令已发送") |
|
|
|
|
|
result.success(true) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* 停止语音交互服务 |
|
|
* 停止语音交互服务 |
|
|
*/ |
|
|
*/ |
|
|
private fun stopVoiceInteractionService() { |
|
|
private fun stopVoiceInteractionService() { |
|
|
|
|
|
Log.d(TAG, "停止语音交互服务") |
|
|
val serviceIntent = Intent(this, VoiceInteractionService::class.java) |
|
|
val serviceIntent = Intent(this, VoiceInteractionService::class.java) |
|
|
stopService(serviceIntent) |
|
|
stopService(serviceIntent) |
|
|
|
|
|
Log.d(TAG, "语音交互服务停止命令已发送") |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 暂停语音交互(停止TTS和语音识别,但保持服务运行) |
|
|
|
|
|
*/ |
|
|
|
|
|
private fun pauseVoiceInteraction() { |
|
|
|
|
|
Log.d(TAG, "发送暂停语音交互请求") |
|
|
|
|
|
val intent = Intent(this, VoiceInteractionService::class.java).apply { |
|
|
|
|
|
action = VoiceInteractionService.ACTION_PAUSE_VOICE_INTERACTION |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 在 Android O 及以上版本,需要使用 startForegroundService |
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |
|
|
|
|
|
startForegroundService(intent) |
|
|
|
|
|
} else { |
|
|
|
|
|
startService(intent) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 发送聊天记录事件到 Flutter |
|
|
|
|
|
*/ |
|
|
|
|
|
private fun sendChatHistoryEvent(agentId: String, userMessage: String, assistantMessage: String, timestamp: Long) { |
|
|
|
|
|
if (voiceInteractionEventSink == null) { |
|
|
|
|
|
Log.e(TAG, "无法发送聊天记录事件:事件通道未准备好") |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Log.d(TAG, "发送聊天记录事件到Flutter: agentId=$agentId, userMessage=${userMessage.take(20)}..., assistantMessage=${assistantMessage.take(20)}...") |
|
|
|
|
|
|
|
|
|
|
|
runOnUiThread { |
|
|
|
|
|
try { |
|
|
|
|
|
val eventData = mapOf( |
|
|
|
|
|
"type" to "chatHistory", |
|
|
|
|
|
"agentId" to agentId, |
|
|
|
|
|
"userMessage" to userMessage, |
|
|
|
|
|
"assistantMessage" to assistantMessage, |
|
|
|
|
|
"timestamp" to timestamp |
|
|
|
|
|
) |
|
|
|
|
|
voiceInteractionEventSink?.success(eventData) |
|
|
|
|
|
Log.d(TAG, "聊天记录事件已发送到Flutter") |
|
|
|
|
|
} catch (e: Exception) { |
|
|
|
|
|
Log.e(TAG, "发送聊天记录事件到Flutter失败: ${e.message}") |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 发送语音交互事件到 Flutter |
|
|
|
|
|
*/ |
|
|
|
|
|
private fun sendVoiceInteractionEvent(event: Map<String, Any>) { |
|
|
|
|
|
if (voiceInteractionEventSink == null) { |
|
|
|
|
|
Log.e(TAG, "无法发送语音交互事件:事件通道未准备好,事件内容: $event") |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Log.d(TAG, "发送语音交互事件到Flutter: $event") |
|
|
|
|
|
|
|
|
|
|
|
runOnUiThread { |
|
|
|
|
|
try { |
|
|
|
|
|
voiceInteractionEventSink?.success(event) |
|
|
|
|
|
Log.d(TAG, "语音交互事件已发送到Flutter") |
|
|
|
|
|
} catch (e: Exception) { |
|
|
|
|
|
Log.e(TAG, "发送语音交互事件到Flutter失败: ${e.message}", e) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
override fun onDestroy() { |
|
|
override fun onDestroy() { |
|
|
|
|
|
// 注销广播接收器 |
|
|
|
|
|
try { |
|
|
|
|
|
unregisterReceiver(voiceInteractionReceiver) |
|
|
|
|
|
} catch (e: Exception) { |
|
|
|
|
|
Log.e(TAG, "注销广播接收器失败: ${e.message}") |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
azureAsrHelper.dispose() |
|
|
azureAsrHelper.dispose() |
|
|
azureTtsHelper.dispose() |
|
|
azureTtsHelper.dispose() |
|
|
super.onDestroy() |
|
|
super.onDestroy() |
|
|
|