Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS background audio stops when screen is locked

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

like image 203
Fritz Menzer Avatar asked Jun 18 '12 14:06

Fritz Menzer


1 Answers

This article explains the problem:

Technical Q&A QA1606 Audio Unit Processing Graph - Ensuring audio playback continues when screen is locked

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));
like image 139
Frank Krueger Avatar answered Nov 02 '22 23:11

Frank Krueger