iOS 语音套件\xef\xbc\x8c SFSpeechRecognizer 和 AVAudioEngine 协同工作进行语音识别,当麦克风被其他进程使用时有时会崩溃。
\n\n self.audioEngine = [[AVAudioEngine alloc] init];\n AVAudioInputNode *inputNode = self.audioEngine.inputNode;\n AVAudioFormat *nativeAudioFormat = [inputNode outputFormatForBus:0];\n __weak typeof(self)weakSelf = self;\n [inputNode installTapOnBus:0 bufferSize:1024 format:nativeAudioFormat block:^(AVAudioPCMBuffer * _Nonnull buffer, AVAudioTime * _Nonnull when) {\n [weakSelf.recognitionRequest appendAudioPCMBuffer:buffer];\n }];\n [self.audioEngine prepare];\n [self.audioEngine startAndReturnError:&error];\nRun Code Online (Sandbox Code Playgroud)\n\n由于未捕获的异常“com.apple.coreaudio.avfaudio”而终止应用程序,原因:“所需条件为假:IsFormatSampleRateAndChannelCountValid(格式)”
\n小智 17
你如何设置你的 AVAudioSession ?此错误通常是由于未正确设置而引起的。
也就是说,您需要在每次使用麦克风之前调用以下代码(或根据您的用例类似的代码),以确保正确设置音频会话。如果不是,例如,您正在使用 .playback 类别并尝试使用麦克风,您将遇到 IsFormatSampleRateAndChannelCountValid(format) 崩溃。
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(.playAndRecord, options: .defaultToSpeaker)
try audioSession.setActive(true, options: .notifyOthersOnDeactivation)
catch let error as NSError {
print("ERROR:", error)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8166 次 |
| 最近记录: |