屏幕锁定时,iOS背景音频停止

我正在尝试让我的音频应用在后台播放。 到目前为止,我在info.plist中将“app播放音频”添加到“所需的背景模式”,并在启动声音生成器之前添加了以下代码:

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

但是,这仅在我切换到另一个应用程序或iPod的主屏幕时才有效。 当我关闭屏幕时,它也会关闭我的音频输出,这绝对不是我想要的。 但是,有关stackoverflow问题的所有教程/文档/答案似乎都表明,当您获得背景音频时,屏幕关闭时保持音频运行会自动出现。 有人对我有暗示吗? 非常感谢提前! 弗里茨

本文解释了这个问题:

技术问答QA1606音频单元处理图 – 确保在屏幕锁定时继续播放音频

基本上,您只需要确保将所有AudioUnits设置为支持4096个切片:

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