|
|
|
@ -178,7 +178,7 @@ object BleService { |
|
|
|
*/ |
|
|
|
fun initialize(appContext: Context): Boolean { |
|
|
|
if (isInitialized) return true |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
this.context = appContext.applicationContext |
|
|
|
// 初始化蓝牙管理器和适配器 |
|
|
|
@ -208,7 +208,7 @@ object BleService { |
|
|
|
recordfile1 = RecordingFile(this.context) |
|
|
|
recordfile1!!.fileName = "重新压缩" |
|
|
|
isInitialized = true |
|
|
|
Log.d(TAG, "BLE服务初始化成功") |
|
|
|
startOpusStreamDecoding(false, 1, 16000, 40) |
|
|
|
return true |
|
|
|
} catch (e: Exception) { |
|
|
|
Log.e(TAG, "BLE服务初始化失败: ${e.message}", e) |
|
|
|
@ -838,22 +838,7 @@ startBytesStatistics() |
|
|
|
recordfile?.saveAudioDataToWav(data) |
|
|
|
// 将完整的帧加入队列,由专门的线程处理 |
|
|
|
audioDataQueue.offer(data) |
|
|
|
// // 将接收到的数据添加到缓存中 |
|
|
|
// synchronized(audioDataBuffer) { |
|
|
|
// audioDataBuffer.addAll(data.toList()) |
|
|
|
|
|
|
|
// // 当缓存达到阈值时,提取完整的帧进行解码 |
|
|
|
// while (audioDataBuffer.size >= AUDIO_BUFFER_SIZE) { |
|
|
|
// // 提取一个完整的帧(1280字节) |
|
|
|
// val frameData = ByteArray(AUDIO_BUFFER_SIZE) |
|
|
|
// for (i in 0 until AUDIO_BUFFER_SIZE) { |
|
|
|
// frameData[i] = audioDataBuffer.removeAt(0) |
|
|
|
// } |
|
|
|
|
|
|
|
|
|
|
|
// Log.d(TAG, "缓存达到阈值,提取 ${frameData.size} 字节帧进行解码") |
|
|
|
// } |
|
|
|
// } |
|
|
|
|
|
|
|
} else { |
|
|
|
// Log.d(TAG, "Opus解码流未启动,忽略音频数据") |
|
|
|
} |
|
|
|
@ -1243,7 +1228,7 @@ startBytesStatistics() |
|
|
|
// Opus 解码相关 |
|
|
|
// ====================================================================================================== |
|
|
|
|
|
|
|
private fun startOpusStreamDecoding( |
|
|
|
public fun startOpusStreamDecoding( |
|
|
|
hasHeader: Boolean = false, // 通常BLE传输的Opus没有文件头 |
|
|
|
channel: Int = 1, |
|
|
|
sampleRate: Int = 16000, // 确认设备端Opus编码采样率 |
|
|
|
@ -1634,7 +1619,12 @@ startBytesStatistics() |
|
|
|
) // 或其他默认声道 |
|
|
|
) |
|
|
|
} |
|
|
|
|
|
|
|
fun closeCodec1(): Boolean { |
|
|
|
recordfile1!!.closeFile() |
|
|
|
|
|
|
|
recordfile!!.closeFile() |
|
|
|
return true |
|
|
|
} |
|
|
|
/** |
|
|
|
* 控制编解码 - 打开解码 |
|
|
|
*/ |
|
|
|
@ -1707,16 +1697,20 @@ startBytesStatistics() |
|
|
|
*/ |
|
|
|
fun openEncoder(): Boolean { |
|
|
|
Log.i(TAG, "打开编码0xB1") |
|
|
|
|
|
|
|
startOpusStreamDecoding(false, 1, 16000, 40) |
|
|
|
// 目前仅发送命令通知设备开始编码。 |
|
|
|
return sendCommand( |
|
|
|
BleConst.CMD_CONTROL_CODEC.toByte(), |
|
|
|
byteArrayOf( |
|
|
|
BleConst.CODEC_CONTROL_ENCODE_ON.toByte(), |
|
|
|
BleConst.AUDIO_CHANNEL_RIGHT.toByte() // 或设备期望的声道 |
|
|
|
) |
|
|
|
) |
|
|
|
recordfile1!!.closeFile() |
|
|
|
recordfile1!!.creatingFiles() |
|
|
|
recordfile!!.closeFile() |
|
|
|
recordfile!!.creatingFiles() |
|
|
|
// startOpusStreamDecoding(false, 1, 16000, 40) |
|
|
|
// // 目前仅发送命令通知设备开始编码。 |
|
|
|
// return sendCommand( |
|
|
|
// BleConst.CMD_CONTROL_CODEC.toByte(), |
|
|
|
// byteArrayOf( |
|
|
|
// BleConst.CODEC_CONTROL_ENCODE_ON.toByte(), |
|
|
|
// BleConst.AUDIO_CHANNEL_RIGHT.toByte() // 或设备期望的声道 |
|
|
|
// ) |
|
|
|
// ) |
|
|
|
return true |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -2102,7 +2096,7 @@ class RecordingFile(private val context: Context) { |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 保存音频数据到 WAV 文件(异步) |
|
|
|
* 保存音频数据到 opus 文件(异步) |
|
|
|
*/ |
|
|
|
internal fun saveAudioDataToWav(buffer: ByteArray) { |
|
|
|
if (fos == null || currentAudioFile == null) return |
|
|
|
|