Browse Source

fix(agent_service): 优化语音识别启动逻辑并添加调试日志

- 调整初始化检查顺序,避免在未初始化时执行蓝牙操作
- 添加 ble_wakeup 模式连续触发保护,防止重复启动
- 增加关键节点调试日志,便于问题排查
- 移除部分冗余日志输出
weicu
liwei1dao 9 months ago
parent
commit
abc266c177
  1. 29
      local_plugins/agent_service/ios/agent_service/Sources/agent_service/AgentServiceImpl.swift
  2. 2
      local_plugins/ble_service/ios/ble_service/Sources/ble_service/BleService.swift

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

@ -442,7 +442,7 @@ class AgentServiceImpl: NSObject {
self.idleTimer = nil self.idleTimer = nil
} }
guard self.isRecognizing else { return } guard self.isRecognizing else { return }
os_log("启动空闲检测,超时时间: %.0f秒,模式: %{public}@", os_log("liwei--------启动空闲检测,超时时间: %.0f秒,模式: %{public}@",
log: self.logger, log: self.logger,
type: .info, type: .info,
idleSeconds, idleSeconds,
@ -453,7 +453,7 @@ class AgentServiceImpl: NSObject {
guard let self = self else { return } guard let self = self else { return }
DispatchQueue.main.async { [weak self] in DispatchQueue.main.async { [weak self] in
guard let self = self else { return } guard let self = self else { return }
os_log("空闲检测触发自动停止,模式: %{public}@", os_log("liwei--------空闲检测触发自动停止,模式: %{public}@",
log: self.logger, log: self.logger,
type: .info, type: .info,
self.currentRecognitionMode) self.currentRecognitionMode)
@ -490,6 +490,17 @@ class AgentServiceImpl: NSObject {
} }
} }
func startRecognition(useBle: Bool = false, mode: String = "normal") -> Bool { func startRecognition(useBle: Bool = false, mode: String = "normal") -> Bool {
if !isInitialized {
sendError("服务未初始化", code: "NOT_INITIALIZED")
return false
}
if useBle, mode == "ble_wakeup", currentRecognitionMode == "ble_wakeup", (isRecognizing || isStartingRecognition) {
os_log("liwei-------- ble_wakeup 连续触发,识别中/启动中,忽略重复 startRecognition", log: logger, type: .info)
audioPlayer?.playStartSound()
return true
}
// 接收到唤醒信号,打开编码器 (设备侧) // 接收到唤醒信号,打开编码器 (设备侧)
print("ai启动语音\(useBle))") print("ai启动语音\(useBle))")
var openResult = false // 添加openResult变量定义 var openResult = false // 添加openResult变量定义
@ -498,10 +509,6 @@ class AgentServiceImpl: NSObject {
// 捕获openEncoder的返回值 // 捕获openEncoder的返回值
openResult = BleService.shared.openEncoder() // 正确捕获返回值 openResult = BleService.shared.openEncoder() // 正确捕获返回值
} }
if !isInitialized {
sendError("服务未初始化", code: "NOT_INITIALIZED")
return false
}
if !useBle { if !useBle {
guard AVAudioSession.sharedInstance().recordPermission == .granted else { guard AVAudioSession.sharedInstance().recordPermission == .granted else {
@ -509,6 +516,7 @@ class AgentServiceImpl: NSObject {
return false return false
} }
} }
lastRecognitionMode = currentRecognitionMode lastRecognitionMode = currentRecognitionMode
stopRecognition() stopRecognition()
if isAiStreaming { if isAiStreaming {
@ -528,13 +536,13 @@ class AgentServiceImpl: NSObject {
// 设置当前识别模式 // 设置当前识别模式
currentRecognitionMode = mode currentRecognitionMode = mode
os_log("liwei--------------- 设置语音识别模式: %{public}@", log: logger, type: .info, mode)
let audioSourceType: AzureAsrHelper.AudioSourceType = useBle ? .external : .microphone let audioSourceType: AzureAsrHelper.AudioSourceType = useBle ? .external : .microphone
// 新增:标记为"启动中",用于允许 stop 在启动未完成时也能生效 // 新增:标记为"启动中",用于允许 stop 在启动未完成时也能生效
isStartingRecognition = true isStartingRecognition = true
print("ai启动语音=\(audioSourceType)=\(useBle)") print("ai启动语音=\(audioSourceType)=\(useBle)")
guard let success = azureAsrHelper?.startContinuousRecognition( guard let success = azureAsrHelper?.startContinuousRecognition(
mod:self.currentRecognitionMode, mod:self.currentRecognitionMode,
audioSourceType: audioSourceType, audioSourceType: audioSourceType,
@ -543,11 +551,13 @@ class AgentServiceImpl: NSObject {
// 新增:启动失败时复位"启动中"状态 // 新增:启动失败时复位"启动中"状态
isStartingRecognition = false isStartingRecognition = false
sendError("启动语音识别失败", code: "RECOGNITION_START_ERROR") sendError("启动语音识别失败", code: "RECOGNITION_START_ERROR")
os_log("liwei--------------- 启动语音识别失败: %{public}@", log: logger, type: .info, mode)
return false return false
} }
if(useBle){ if(useBle){
isKeepResult = true isKeepResult = true
} }
os_log("liwei--------------- 启动语音识别: %{public}@", log: logger, type: .info, mode)
// 根据模式启动相应的空闲检测(此处 guard isRecognizing,会在 onSessionStarted 中再次启动) // 根据模式启动相应的空闲检测(此处 guard isRecognizing,会在 onSessionStarted 中再次启动)
startIdleCheckForMode(mode: mode) startIdleCheckForMode(mode: mode)
@ -2125,10 +2135,12 @@ class AudioPlayer {
} }
func playStartSound() { func playStartSound() {
os_log("liwei 播放 playStartSound", log: self.logger, type: .debug)
playSound(named: "start", fileType: "mp3") playSound(named: "start", fileType: "mp3")
} }
func playAwaitSound() { func playAwaitSound() {
os_log("liwei 播放 playAwaitSound", log: self.logger, type: .debug)
// 若已处于等待音效播放状态,则直接返回,避免频繁启停 // 若已处于等待音效播放状态,则直接返回,避免频繁启停
if isAwaitSoundActive { return } if isAwaitSoundActive { return }
@ -2146,6 +2158,7 @@ class AudioPlayer {
} }
func playStopSound() { func playStopSound() {
os_log("liwei 播放 playStopSound", log: self.logger, type: .debug)
playSound(named: "stop", fileType: "mp3") playSound(named: "stop", fileType: "mp3")
} }

2
local_plugins/ble_service/ios/ble_service/Sources/ble_service/BleService.swift

@ -1008,7 +1008,7 @@ private var cmdReplyType: UInt8 = 0
// 记录响应详情 // 记录响应详情
var hexString = data.map { String(format: "0x%02X", $0) }.joined(separator: ", ") var hexString = data.map { String(format: "0x%02X", $0) }.joined(separator: ", ")
os_log("liwei---- 收到设备响应: %@", log: logger, type: .debug, hexString) // os_log("liwei---- 收到设备响应: %@", log: logger, type: .debug, hexString)
// 检查数据长度(与 Android 统一失败判定保持一致) // 检查数据长度(与 Android 统一失败判定保持一致)
if data.count != Int(length) + 4 { // 帧头 + 命令 + 长度 + 数据 + CRC if data.count != Int(length) + 4 { // 帧头 + 命令 + 长度 + 数据 + CRC

Loading…
Cancel
Save