Set iOS background audio programmatically

I want to be able to set the background audio mode programmatically via a setting. Is there currently a way I can do that? I couldn’t find anything.


1 Like

Hi Peter,

This isn’t easy to do with a simple toggle - changing the background audio setting requires changes in both the XCode project configuration and the app’s plist.

the app’s plist merely enables background audio. You can then decide to turn audio off in applicationDidEnterBackground. The juce audiobus implementation that’s somewhere here in the forum does that.
Problem is, there don’t seem to be any flags in the AudioIODevice class, for my app to specify whether or not to keep playing audio, and I haven’t found any way to get a handle of the iOS specific juce classes.
Has anybody found a solution for this problem?

I’m a little confused as I’ve seen this as a runtime setting with Garageband and Cubasis.

Sorry, I misinterpreted the question - I thought you were asking about generating projects programatically.

I don’t think there’s a runtime switch you can use to toggle the background audio mode on iOS. You can achieve the same effect using the suspended() and resumed() methods of your app (via JUCEApplication) and the closeAudioDevice() and restartLastAudioDevice() methods of an AudioDeviceManager.

those functions don’t keep the AudioIODevice class from turning off the audio.
I got it working by adding a flag inside AudioIODevice. I was hoping I could accomplish this without altering the juce codebase.

I think I’m still missing something…

If you enable background audio in your XCode project then the audio will keep playing when you switch to another application.

If you want to have a run-time toggle then you could achieve this by enabling background audio in XCode, then manually stopping the audio on suspended() and resumed().

I think I see now where your confusion is coming from…
I was working with a patched juce_ios_Audio.cpp implementation, that has audiobus support, and it turns off audio depending on audiobus related values.
While that implementation works, it’s somewhat dirty (as I for example couldn’t just change the behavior from outside of the class)