From 01fd11a4730edb289ea9ada042de88ede005d1dd Mon Sep 17 00:00:00 2001 From: fdp <1286779656@qq.com> Date: Sat, 9 Aug 2025 17:35:27 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E6=88=90120=E5=AD=97=E8=8A=82?= =?UTF-8?q?=E5=8F=91=E9=80=81opus?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controllers/translation_controller.dart | 14 +++++ .../yunqiinnovation/ble_service/BleService.kt | 55 ++++++++++++++++--- 2 files changed, 62 insertions(+), 7 deletions(-) diff --git a/lib/modules/translation/controllers/translation_controller.dart b/lib/modules/translation/controllers/translation_controller.dart index 69a6ba7d6..3751c3cca 100644 --- a/lib/modules/translation/controllers/translation_controller.dart +++ b/lib/modules/translation/controllers/translation_controller.dart @@ -1132,6 +1132,9 @@ class TranslationController extends GetxController { // 重新初始化ASR服务以支持新的语言 Future _reinitializeAsrService() async { try { + // 停止当前识别 + await stopRecognition(); + // 更新ASR支持的语言 final List asrSupportedLanguages = [ sourceLanguageCode.value, @@ -1144,6 +1147,17 @@ class TranslationController extends GetxController { await _asrService.initialize( supportedLanguages: asrSupportedLanguages, ); +// Future initialize({ +// required String subscriptionKey, +// required String region, +// required List supportedLanguages, +// required String audioSourceType, +// required String translationAccessKey, +// required String translationSecretKey, +// String translationRegion = 'cn-north-1', +// }); + // 重新初始化ASR服务以支持通话音频源 + await _astService.initialize(supportedLanguages: asrSupportedLanguages); // 获取识别事件流 - 这启动了异步识别过程 var recognitionStream = await _asrService.recognizeCallback(); // 监听识别结果 diff --git a/local_plugins/ble_service/android/src/main/kotlin/com/yunqiinnovation/ble_service/BleService.kt b/local_plugins/ble_service/android/src/main/kotlin/com/yunqiinnovation/ble_service/BleService.kt index b56963757..296c36d45 100644 --- a/local_plugins/ble_service/android/src/main/kotlin/com/yunqiinnovation/ble_service/BleService.kt +++ b/local_plugins/ble_service/android/src/main/kotlin/com/yunqiinnovation/ble_service/BleService.kt @@ -126,11 +126,15 @@ object BleService { private val isAudioSending = AtomicBoolean(false) // 音频数据分块发送的常量 - private val AUDIO_CHUNK_SIZE = 80 // 每次发送80字节 - private val AUDIO_SEND_INTERVAL = 40L // 发送间隔20ms + private val AUDIO_CHUNK_SIZE = 120 // 每次发送80字节 + private val AUDIO_SEND_INTERVAL = 60L // 发送间隔20ms // 音频数据缓冲区,用于累积数据到80字节再发送 private val audioBuffer = mutableListOf() + + // 重发机制相关常量 + private val MAX_RETRY_COUNT = 1 // 最大重试次数 + private val RETRY_DELAY = 30L // 重试延迟时间(毫秒) // 初始化状态 private var isInitialized = false @@ -1393,10 +1397,10 @@ startBytesStatistics() audioBuffer.addAll(data.toList()) // 当缓冲区达到80字节时,发送数据 - while (audioBuffer.size >= 80) { + while (audioBuffer.size >= AUDIO_CHUNK_SIZE) { // 取出80字节数据 - val chunk = ByteArray(80) - for (i in 0 until 80) { + val chunk = ByteArray(AUDIO_CHUNK_SIZE) + for (i in 0 until AUDIO_CHUNK_SIZE) { chunk[i] = audioBuffer.removeAt(0) } @@ -1465,10 +1469,11 @@ startBytesStatistics() } /** - * 发送音频数据块到设备 + * 发送音频数据块到设备(带重发机制) * @param chunk 要发送的音频数据块 + * @param retryCount 当前重试次数 */ - private fun sendAudioChunk(chunk: ByteArray) { + private fun sendAudioChunk(chunk: ByteArray, retryCount: Int = 0) { try { if (callWriteChar == null || bluetoothGatt == null) { Log.e(TAG, "蓝牙连接或特征值未准备就绪") @@ -1486,6 +1491,7 @@ startBytesStatistics() bytesSentInCurrentSecond += chunk.size Log.d(TAG, "成功发送音频数据块,大小: ${chunk.size} 字节") } else { + Log.e(TAG, "发送音频数据块失败,大小: ${chunk.size} 字节") // 详细的失败原因分析 val failureReason = StringBuilder("发送音频数据块失败,大小: ${chunk.size} 字节") @@ -1537,9 +1543,44 @@ startBytesStatistics() } Log.e(TAG, failureReason.toString()) + + // // 实现重发机制 + // if (retryCount < MAX_RETRY_COUNT) { + // Log.w(TAG, "准备重发音频数据块,重试次数: ${retryCount + 1}/$MAX_RETRY_COUNT") + + // // 延迟后重发 + // audioSendHandler.postDelayed({ + // val isSuccess = bluetoothGatt?.writeCharacteristic(callWriteChar) + + // if (isSuccess == true) { + // Log.d(TAG, "重发音频数据块成功") + // } else { + // Log.e(TAG, "重发音频数据块失败") + // } + // }, RETRY_DELAY) + // } else { + // Log.e(TAG, "音频数据块发送失败,已达到最大重试次数($MAX_RETRY_COUNT),丢弃数据块") + // // 可以选择将失败的数据块记录到日志或进行其他处理 + // } } } catch (e: Exception) { Log.e(TAG, "发送音频数据块异常: ${e.message}", e) + + // // 异常情况下也进行重发 + // if (retryCount < MAX_RETRY_COUNT) { + // Log.w(TAG, "发送异常,准备重发音频数据块,重试次数: ${retryCount + 1}/$MAX_RETRY_COUNT") + // audioSendHandler.postDelayed({ + // val isSuccess = bluetoothGatt?.writeCharacteristic(callWriteChar) + + // if (isSuccess == true) { + // Log.d(TAG, "重发音频数据块成功") + // } else { + // Log.e(TAG, "重发音频数据块失败") + // } + // }, RETRY_DELAY) + // } else { + // Log.e(TAG, "音频数据块发送异常,已达到最大重试次数($MAX_RETRY_COUNT),丢弃数据块") + // } } } } catch (e: Exception) {