Short small starter guide for AVAudioEngine and AVAudioSession on iOS

AVAudioEngine and AVAudioSession

Welcome! I will start off with the terms AVAudioEngineImpl::Initialize(NSError**).

Why? I want to make those who run into this issue have to possibility to find this post through Search Engines!

This is short small breakdown based on what I observed while trying to use these two Components. It's not a guide that goes into all the details.

If you're trying to figure out how to fix a crash, you may can find a common way to fix it, in this post!

Is it possible to use AVAudioEngine and AVAudioSession together?

The answer is yes.

But you will face challenges regarding it. Mostly AVAudioEngine. Whatever you're trying to do, it will take a lot of testing. I don't know how it will be with an IDE. But with just .app and iPhone it will take some testing. Or a lot of testing.

Something that helped me fixing a crash was, this here: https://developer.apple.com/documentation/avfaudio/audio_engine/audio_units/using_voice_processing

This example Project by Apple, uses both AVAudioEngine and AVAudioSession.

How can I fix AVAudioEngineImpl::Initialize(NSError**) ?

I think this depends. If you're lucky and have a crash log, you may can find clues, but the stack trace sometimes doesn't really help either.

I will mention common cases that I encountered though.

inputNode

https://developer.apple.com/documentation/avfaudio/avaudioengine/1386063-inputnode

You need an inputNode apparently. You need to access it or else I think there won't be one. And if there isn't one, AVAudioEngine.start will most likely crash.

The audio engine creates a singleton on demand when first accessing this variable.

Doing this has prevented this common issue for me.

.prepare deallocates and can cause a crash if you restart your AudioEngine

Another issue I faced was handling .prepare wrong. You don't need .prepare. But if you use installTap or other things, I think you need it.

Here is a common thing to note.

  • If you had previous initialized inputNode. Those could be gone after using .prepare.
  • You have to ensure you're accessing AVAudioEngine.inputNode again before calling .start() or whatever node you need.

The Voice Processing Project, does this by creating a Managing Controller for AVAudioEngine with a sort of "setup" function, which ensures that everything is ready, before .prepare and .start get called.

AVAudioSession's setCategory

You have to experiment with it. The crashes can be very weird. Sometimes your App will only crash once, and then only after you install it again, or if you start it up.

You are actually able to use .setActive and .setCategory with AVAduioEngine. Just do not try to do .setActive(false) before you've stopped the AudioEngine, as it will fail.

Sometimes I'd run into an issue with .setActive(true) so you really have to experiment if leaving that part out resolves the issue or not.

try session.setCategory(.multiRoute, mode: .default, options: [.defaultToSpeaker, .mixWithOthers])

Experiment with it. But these .multiRoute and .mixWithOthers have allowed me to use AVAudioEngine to make a test recording. And I can even switch the Data Sources and Polar Patterns without any issues.

Sometimes you can get away without setting .setActive at all. Not sure if AVAudioEngine does it automatically.

Short Summary

  • If you use .prepare and then .stop, make sure to initialize things like .inputNode before calling .prepare and .start again. (THIS CAN BE DIFFERENT)
  • Only call .setActive(false) after you used .stop. Otherwise I believe it has no chance to stop it.
  • AVAudioSession setCategory is important. Ensure you use mixRoutes or experiment with all the modes.
    • If you manage to solve your crash, you'll be able to indeed change the Data Sources and Polar Patterns and more!
  • Use isRunning before using .start, this will save you from another crash. If you use .start while it's already running, I think try and catch won't save you here, you have to ensure you're not starting it twice.


I hope that this short breakdown will help you to resolve your crash. If you get deeper into AVAudioEngine and AVAudioSession, you'll probably face more crashes. I yet, need to figure out how to solve them. I have a lot of trouble to put my Testing App on my iPhone, so I am sorry if this guide didn't cover every detail of it.

A HUGE tip from me is to check the Documentations. As example, when I read the Documentation for inputNode I learned why my app crashed, it's because I never accessed and initialized one.

The Developer Documentation can be a little bit of a laberynth, and I strongly recommend you to read every property you try to access if you believe they cause issues. And I also recommend to find example Projects like the Voice Processing ones. As there aren't any Code Examples in the Documentation.

Short small starter guide for AVAudioEngine and AVAudioSession on iOS
 
 
Q