iOS background audio stops when screen is locked

感情迁移 提交于 2019-11-30 16:58:40

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/11085007/ios-background-audio-stops-when-screen-is-locked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!