I'm trying to get my audio app to play in the background. So far I added "app plays audio" to the "required background modes" in info.plist and also the following code just before starting my sound generator:
AudioSessionInitialize(NULL, kCFRunLoopDefaultMode, &interruptionListener, sgD);
AudioSessionAddPropertyListener(kAudioSessionProperty_AudioRouteChange, &routeChangeListener, sgD);
// select "Playback" audio session category
NSError *setCategoryError = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &setCategoryError];
OSStatus propertySetError = 0;
UInt32 category = kAudioSessionCategory_MediaPlayback;
propertySetError = AudioSessionSetProperty ( kAudioSessionProperty_AudioCategory, sizeof (category), &category );
AudioSessionSetActive(true);
However, this works only when I switch to another app or to the iPod's main screen. When I turn off the screen, it also turns off my audio output, which is definitely not what I want. However, all tutorials / documentation / answers to questions on stackoverflow seem to indicate that keeping the audio running while the screen is off comes automatically when you get the background audio to work. Does anybody have a hint for me? Thanks a lot in advance! Fritz
This article explains the problem:
Basically, you just need to make sure you set all AudioUnits to support 4096 slices:
// set the mixer unit to handle 4096 samples per slice since we want to keep rendering during screen lock
UInt32 maxFPS = 4096;
AudioUnitSetProperty(
mMixer,
kAudioUnitProperty_MaximumFramesPerSlice,
kAudioUnitScope_Global,
0,
&maxFPS,
sizeof(maxFPS));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With