AVAudioEngine.connect 在硬件而非模拟器上崩溃

AVAudioEngine.connect crash on hardware not simulator

var engine:AVAudioEngine!
var format = engine.inputNode.inputFormat(forBus: 0)
engine.connect(engine.inputNode, to: engine.mainMixerNode, format: format)

在函数 AVAudioEngine.connect 中使我的应用程序仅在硬件上崩溃 但在模拟器中没问题。

当我在硬件上 运行 时出现错误提示。

Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'required condition is false: IsFormatSampleRateAndChannelCountValid(format)'
terminating with uncaught exception of type NSException

我明白了。 我只是将 AVAudioSession 类别类型设置错误。 如果您遇到相同的错误,请查看 AVAudioSession 并确保您的 AVAudioSession 类别处于麦克风权限的正确设置中。 例如

AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord, with: .mixWithOthers)

使用此设置将允许应用同时使用麦克风和播放声音。