Browse Source

优化 播放逻辑和卡顿问题

newdev_shunjiawei
liwei1dao 1 year ago
parent
commit
ea3d35d7eb
  1. 4
      lib/modules/agent/controllers/agent_controller.dart
  2. 11
      local_plugins/agent_service/ios/agent_service/Sources/agent_service/AgentServiceImpl.swift
  3. 3
      local_plugins/azure_speech/ios/azure_speech/Sources/azure_speech/AzureAsrHelper.swift
  4. 2
      local_plugins/azure_speech/ios/azure_speech/Sources/azure_speech/AzureTtsHelper.swift
  5. 24
      local_plugins/azure_speech/ios/azure_speech/Sources/tools/SimpleAudioReceiver.swift

4
lib/modules/agent/controllers/agent_controller.dart

@ -1596,7 +1596,7 @@ class AgentController extends GetxController {
isPushToTalkActive.value = true; isPushToTalkActive.value = true;
//await AgentService.recognizeCallback(); //await AgentService.recognizeCallback();
// 开始语音识别 // 开始语音识别
await AgentService.startConversation(mode: 'push_to_talk'); AgentService.startConversation(mode: 'push_to_talk');
// await startVoiceInput(); // await startVoiceInput();
} }
@ -1610,7 +1610,7 @@ class AgentController extends GetxController {
// 清除按住说话状态(但保持按住说话模式) // 清除按住说话状态(但保持按住说话模式)
isPushToTalkActive.value = false; isPushToTalkActive.value = false;
await AgentService.stopConversation(); AgentService.stopConversation();
// 停止语音识别 // 停止语音识别
// await stopVoiceInput(); // await stopVoiceInput();
} }

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

@ -396,7 +396,7 @@ class AgentServiceImpl: NSObject {
} }
print("ai启动语音=\(audioSourceType)=\(useBle)") print("ai启动语音=\(audioSourceType)=\(useBle)")
guard let success = azureAsrHelper?.startContinuousRecognition( guard let success = azureAsrHelper?.startContinuousRecognition(
// callback: self, // callback: self,
audioSourceType: audioSourceType audioSourceType: audioSourceType
), success else { ), success else {
sendError("启动语音识别失败", code: "RECOGNITION_START_ERROR") sendError("启动语音识别失败", code: "RECOGNITION_START_ERROR")
@ -1708,7 +1708,10 @@ extension AgentServiceImpl: AzureAsrHelper.ContinuousRecognizeCallback {
hasSpeechDetected = false hasSpeechDetected = false
// 使用模式感知的空闲检测启动方法 // 使用模式感知的空闲检测启动方法
startIdleCheckForMode(mode: currentRecognitionMode) startIdleCheckForMode(mode: currentRecognitionMode)
audioPlayer?.playStartSound() if (currentRecognitionMode == "ble_wakeup") {
audioPlayer?.playStartSound()
}
} }
func onSessionStopped() { func onSessionStopped() {
@ -1716,7 +1719,9 @@ extension AgentServiceImpl: AzureAsrHelper.ContinuousRecognizeCallback {
isRecognizing = false isRecognizing = false
hasSpeechDetected = false hasSpeechDetected = false
stopIdleCheck() stopIdleCheck()
audioPlayer?.playStopSound() if (currentRecognitionMode == "ble_wakeup") {
audioPlayer?.playStopSound()
}
} }
func onCanceled(_ reason: String, _ errorDetails: String) { func onCanceled(_ reason: String, _ errorDetails: String) {

3
local_plugins/azure_speech/ios/azure_speech/Sources/azure_speech/AzureAsrHelper.swift

@ -703,9 +703,6 @@ public class AzureAsrHelper: NSObject {
* 重命名指定路径的音频文件 * 重命名指定路径的音频文件
*/ */
public func renameFile(filePath: String, newName: String)-> Bool { public func renameFile(filePath: String, newName: String)-> Bool {
audioStream?.recordfile?.renameFile(at: filePath, to: newName) // Fixed method call audioStream?.recordfile?.renameFile(at: filePath, to: newName) // Fixed method call
return true return true
} }

2
local_plugins/azure_speech/ios/azure_speech/Sources/azure_speech/AzureTtsHelper.swift

@ -16,7 +16,7 @@ public class AzureTtsHelper: NSObject, ITtsService {
private var synthesizer: SPXSpeechSynthesizer? private var synthesizer: SPXSpeechSynthesizer?
private var isInitialized = false private var isInitialized = false
private var speaking = false private var speaking = false
private var sessionid = "" //语音播报任务id private var sessionid = "" //语音播报任务idsetAudioOutputRoute
// 当前配置 // 当前配置
private var currentVoice = DEFAULT_VOICE private var currentVoice = DEFAULT_VOICE

24
local_plugins/azure_speech/ios/azure_speech/Sources/tools/SimpleAudioReceiver.swift

@ -70,8 +70,9 @@ public class SimpleAudioReceiver: NSObject {
print("音频录制组件已初始化,跳过重复初始化") print("音频录制组件已初始化,跳过重复初始化")
return return
} }
preConfigureAudioSession()
print("初始化了") // 预配置音频会话
audioFormat = getOptimalAudioFormat() audioFormat = getOptimalAudioFormat()
pushAudioStream = SPXPushAudioInputStream() pushAudioStream = SPXPushAudioInputStream()
@ -84,6 +85,22 @@ public class SimpleAudioReceiver: NSObject {
startWriteThread() startWriteThread()
} }
// 【新增】预配置音频会话方法
/**
* 预配置音频会话,在应用启动时调用以减少首次录音延迟
* 【优化重点】将耗时的音频会话配置提前到初始化阶段
*/
private func preConfigureAudioSession() {
do {
// 设置音频会话类别和模式
runMicrophoneCapture()
stopMicrophoneCapture()
print("音频会话预配置完成")
} catch {
print("音频会话预配置失败: \(error)")
}
}
/// 获取最佳音频格式 (iOS 通常支持标准采样率) /// 获取最佳音频格式 (iOS 通常支持标准采样率)
public func getOptimalAudioFormat() -> AVAudioFormat? { public func getOptimalAudioFormat() -> AVAudioFormat? {
let sampleRate: Double = 16000 // iOS 通常支持 16kHz let sampleRate: Double = 16000 // iOS 通常支持 16kHz
@ -373,7 +390,7 @@ public class SimpleAudioReceiver: NSObject {
_isWriting=false _isWriting=false
audioEngine?.pause() audioEngine?.pause()
print("stopMicrophoneCapture") print("stopMicrophoneCapture")
} }
public func releaseAudioResources() { public func releaseAudioResources() {
@ -395,6 +412,7 @@ public class SimpleAudioReceiver: NSObject {
case receiver case receiver
case bluetooth case bluetooth
} }
public func setAudioOutputRoute(_ route: AudioOutputRoute) { public func setAudioOutputRoute(_ route: AudioOutputRoute) {
do { do {
@ -403,7 +421,7 @@ public class SimpleAudioReceiver: NSObject {
print("当前正在识别") print("当前正在识别")
try audioEngine?.pause() try audioEngine?.pause()
// 先停用以避免冲突 // 先停用以避免冲突
try audioSession.setActive(false) // try audioSession.setActive(false)
} }
print("setAudioOutputRoutecurrent,route=\(route)") print("setAudioOutputRoutecurrent,route=\(route)")

Loading…
Cancel
Save