Set iOS background audio programmatically


#1

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.

Peter


#2

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.


#3

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?


#4

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


#5

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.


#6

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.


#7

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().


#8

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)