AudioKit / AudioKit

Audio synthesis, processing, & analysis platform for iOS, macOS and tvOS

Home Page:http://audiokit.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Restarting audio engine after stopping on scenePhase change

myreauks opened this issue · comments

macOS Version(s) Used to Build

macOS 14 Sonoma

Xcode Version(s)

Xcode 15

Description

I have a simple system for stopping the audio engine when scenePhase changes to background and then again restarting the engine when scenePhase changes to active. However, after returning from background mode, an Audioplayer node doesn't play anymore before reloading the audiofile.

Here is an example:
https://github.com/myreauks/audioplayer-example/blob/main/audioplayer-example/ContentView.swift

I want to stop the engine when scenePhase changes to background to disable the orange recording dot in the top bar of iOS. Unless of course, there is some other way to disable audio input from microphone to hide the orange recording indicator without stopping the whole engine?

Best,
Miro

Crash Logs, Screenshots or Other Attachments (if applicable)

No response

Try this:

if scenePhase == .active {
            conductor.start()
            print("Start")
} else if scenePhase == .background {
            conductor.player.playerNode.stop()
            conductor.stop()
            print("Stop")
}

Normally calling conductor.player.stop() should do this, but it isn't executed since the status returns .stopped. I'm not sure if this needs to be fixed in AudioKit or not. Calling stop() on a stopped player should be allowed since it resets some other values too, and this is an edge case where it's needed.

Wow, such a simple and small addition but works perfectly. Probably this does not need fixing in the Audiokit, I think it's mode of an issue with lack of documentation regarding stopping and restarting the engine.
Thanks for the help!