objective-c - 所需条件为假 : IsFormatSampleRateAndChannelCountValid(format)

标签 objective-c speech

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/

相关文章:

audio - 哪个功能,算法最适合说话者验证

ios - 如何防止iOS的NSDictionary中的订单更改

ios - 在 spritekit 中保存锁定和解锁的游戏关卡

iphone - 检查设备上是否启用了 facetime

ios - 在 UIScrollView subview 中使用自动布局(以编程方式)

java - 识别使用 Java 的人所说的字母

text-to-speech - 在语音 XML 2.0 提示中使用不同语言

.net - 有更好的 .Net 源 TTS 包吗?

iphone - 用于语音转文本(语音识别)iPhone 的 API 或 SDK

objective-c - Plist不会写入文件