简体   繁体   中英

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

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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM