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)
使用此设置将允许应用同时使用麦克风和播放声音。