|
|
|
@ -14,9 +14,10 @@ import io.flutter.plugin.common.EventChannel |
|
|
|
import kotlinx.coroutines.* |
|
|
|
import kotlin.coroutines.CoroutineContext |
|
|
|
import com.microsoft.cognitiveservices.speech.audio.* |
|
|
|
|
|
|
|
import com.yunqiinnovation.ble_service.BleService |
|
|
|
import com.yunqiinnovation.ble_service.BleConst |
|
|
|
/** AzureSpeechPlugin */ |
|
|
|
class AzureSpeechPlugin: FlutterPlugin, CoroutineScope { |
|
|
|
class AzureSpeechPlugin: FlutterPlugin,BleService.Callback, CoroutineScope { |
|
|
|
private val tag = "AzureSpeechPlugin" |
|
|
|
private lateinit var context: Context |
|
|
|
private val mainHandler = Handler(Looper.getMainLooper()) |
|
|
|
@ -37,7 +38,10 @@ class AzureSpeechPlugin: FlutterPlugin, CoroutineScope { |
|
|
|
private lateinit var ttsEventChannel: EventChannel |
|
|
|
private var ttsEventSink: EventChannel.EventSink? = null |
|
|
|
private lateinit var azureTtsHelper: AzureTtsHelper |
|
|
|
// 是否已注册BLE回调 |
|
|
|
private var isRegisteredToBle = false |
|
|
|
|
|
|
|
|
|
|
|
// ASR 事件发送方法 |
|
|
|
private fun sendAsrEvent(event: Map<String, Any>) { |
|
|
|
if (asrEventSink == null) { |
|
|
|
@ -110,6 +114,19 @@ class AzureSpeechPlugin: FlutterPlugin, CoroutineScope { |
|
|
|
// 初始化Azure语音服务 |
|
|
|
azureTtsHelper = AzureTtsHelper(context) |
|
|
|
azureAsrHelper = AzureAsrHelper(context) |
|
|
|
|
|
|
|
// 2. 初始化BleService并注册回调 |
|
|
|
if (BleService.initialize(context)) { |
|
|
|
if (BleService.addCallback(this)) { |
|
|
|
isRegisteredToBle = true |
|
|
|
FileLogger.d(tag, "已注册BLE回调") |
|
|
|
} else { |
|
|
|
FileLogger.w(tag, "BLE回调注册失败") |
|
|
|
} |
|
|
|
} else { |
|
|
|
FileLogger.e(tag, "BLE服务初始化失败") |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// ASR方法处理器 |
|
|
|
@ -359,7 +376,12 @@ class AzureSpeechPlugin: FlutterPlugin, CoroutineScope { |
|
|
|
try { |
|
|
|
// 取消所有协程 |
|
|
|
job.cancel() |
|
|
|
|
|
|
|
// 取消BLE回调注册 |
|
|
|
if (isRegisteredToBle) { |
|
|
|
BleService.removeCallback(this) |
|
|
|
isRegisteredToBle = false |
|
|
|
} |
|
|
|
|
|
|
|
// 释放资源 |
|
|
|
azureTtsHelper.dispose() |
|
|
|
azureAsrHelper.dispose() |
|
|
|
@ -367,4 +389,34 @@ class AzureSpeechPlugin: FlutterPlugin, CoroutineScope { |
|
|
|
FileLogger.e(tag, "释放资源失败: ${e.message}") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
override fun onScanResult(devices: List<Map<String, Any>>) { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
override fun onConnectionStateChanged(state: Int) { |
|
|
|
// 可选:处理连接状态变化 |
|
|
|
if (state == BleConst.STATE_CONNECTED) { |
|
|
|
FileLogger.d(tag, "Ble设备已连接,准备好语音交互") |
|
|
|
} else if (state == BleConst.STATE_DISCONNECTED) { |
|
|
|
FileLogger.d(tag, "Ble设备已断开,停止语音服务") |
|
|
|
// 设备断开连接,停止语音服务 |
|
|
|
// stopListening() |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
override fun onAudioDataReceived(data: ByteArray) { |
|
|
|
FileLogger.d(tag, "AzureSpeechPlugin onAudioDataReceived, data: ${data.size}") |
|
|
|
azureAsrHelper.pushAudioData(data) |
|
|
|
} |
|
|
|
|
|
|
|
override fun onWakeupSignalReceived() { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
override fun onDeviceInfoReceived(infoType: Int, infoData: Map<String, Any>) { |
|
|
|
|
|
|
|
} |
|
|
|
} |