Search code examples
swiftaudiokit

Determine if AudioKit.start() needs to be called


Using the AudioKit framework, how can I determine if start() has already been called? Also, if I have called start() during the current application session and I don't call stop(), is there a situation where I would need to call start() again?

I'm building an app that plays a lot of samples, and I'm trying to avoid calling start() multiple times unnecessarily.


Solution

  • Looking through the source I was able to determine you can use the following expression:

    if !AudioKit.engine.isRunning {
        try? AudioKit.start()
    }
    

    I'm still curious why calling AudioKit.start() does not check whether the engine is already running.

    edit

    I've also found that it's useful to wrap all calls to AKPlayer.play() with the following check in order to prevent this crash: Required condition is false: _engine->IsRunning().

    if AudioKit.engine.isRunning {
        myAKPlayer.play()
    }