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

in function AVAudioEngine.connect make my app crash only on hardware but in simulator it's fine.

When I run on hardware it's error says.

Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'required condition is false: IsFormatSampleRateAndChannelCountValid(format)'
terminating with uncaught exception of type NSException
  • I'm currently experiencing almost the same issue; except that there's a crash happening on both simulator and hardware: https://stackoverflow.com/questions/58495080/avfoundation-microphone-crash-on-start – narner Oct 21 '19 at 23:58

1 Answers1

2

I figure it out. I just set the AVAudioSession category type wrong. If you have the same error take a look at AVAudioSession and make sure that your AVAudioSession category is in the right setting for microphone permission. for instance

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

with this setting will allow app to use microphone and play sound in the same time.

  • I had "required condition is false: IsFormatSampleRateAndChannelCountValid(format)" issue. And your solution helped me to fix it. Thx. For Swift 5, "AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playAndRecord, options: .mixWithOthers)" – DynamicScope Mar 17 '20 at 11:26