Browse Source

优化 播放逻辑和卡顿问题

newdev_shunjiawei
liwei1dao 1 year ago
parent
commit
ea3d35d7eb
  1. 4
      lib/modules/agent/controllers/agent_controller.dart
  2. 5
      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. 22
      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();
} }

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

@ -1708,16 +1708,21 @@ extension AgentServiceImpl: AzureAsrHelper.ContinuousRecognizeCallback {
hasSpeechDetected = false hasSpeechDetected = false
// 使用模式感知的空闲检测启动方法 // 使用模式感知的空闲检测启动方法
startIdleCheckForMode(mode: currentRecognitionMode) startIdleCheckForMode(mode: currentRecognitionMode)
if (currentRecognitionMode == "ble_wakeup") {
audioPlayer?.playStartSound() audioPlayer?.playStartSound()
} }
}
func onSessionStopped() { func onSessionStopped() {
sendEvent(name: "recognition_stopped", data: ["status": "stopped"]) sendEvent(name: "recognition_stopped", data: ["status": "stopped"])
isRecognizing = false isRecognizing = false
hasSpeechDetected = false hasSpeechDetected = false
stopIdleCheck() stopIdleCheck()
if (currentRecognitionMode == "ble_wakeup") {
audioPlayer?.playStopSound() audioPlayer?.playStopSound()
} }
}
func onCanceled(_ reason: String, _ errorDetails: String) { func onCanceled(_ reason: String, _ errorDetails: String) {
var data: [String: Any] = [:] var data: [String: Any] = [:]

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

22
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
@ -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