iOS语音工具包,SFSpeechRecognizer和AVAudioEngine配合进行语音识别,当麦克风被其他进程使用时有时会崩溃。
self.audioEngine = [[AVAudioEngine alloc] init];
AVAudioInputNode *inputNode = self.audioEngine.inputNode;
AVAudioFormat *nativeAudioFormat = [inputNode outputFormatForBus:0];
__weak typeof(self)weakSelf = self;
[inputNode installTapOnBus:0 bufferSize:1024 format:nativeAudioFormat block:^(AVAudioPCMBuffer * _Nonnull buffer, AVAudioTime * _Nonnull when) {
[weakSelf.recognitionRequest appendAudioPCMBuffer:buffer];
}];
[self.audioEngine prepare];
[self.audioEngine startAndReturnError:&error];
由于未捕获的异常“com.apple.coreaudio.avfaudio”而终止应用程序,原因:“所需条件为假:IsFormatSampleRateAndChannelCountValid(format)”
最佳答案
你如何设置你的 AVAudioSession?此错误通常是由于未正确设置而引起的。
也就是说,您需要在每次使用麦克风之前调用以下代码(或根据您的用例进行类似调用),以确保正确设置 Audio Session 。如果不是,例如,您正在使用 .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)
}
关于objective-c - 所需条件为假 : IsFormatSampleRateAndChannelCountValid(format),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60984704/