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