iOS5,iOS6为什么AVAudioPlayer播放会在屏幕锁定时停止?

iOS5,iOS6为什么AVAudioPlayer播放会在屏幕锁定时停止?,ios5,ios6,avaudioplayer,avaudiosession,Ios5,Ios6,Avaudioplayer,Avaudiosession,我已经为几个iOS应用程序使用了AVAudioPlayer,它们都在屏幕锁定后成功播放,直到我的iPad2上的iOS6(或iOS5),现在它们停止播放。下面是我如何设置它的,它过去工作得很好: // Registers this class as the delegate of the audio session. [[AVAudioSession sharedInstance] setDelegate: self]; // Use this code instead

我已经为几个iOS应用程序使用了AVAudioPlayer,它们都在屏幕锁定后成功播放,直到我的iPad2上的iOS6(或iOS5),现在它们停止播放。下面是我如何设置它的,它过去工作得很好:

    // Registers this class as the delegate of the audio session.
    [[AVAudioSession sharedInstance] setDelegate: self];

    // Use this code instead to allow the app sound to continue to play when the screen is locked.
    [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];

    UInt32 doSetProperty = 0;
    AudioSessionSetProperty ( kAudioSessionProperty_OverrideCategoryMixWithOthers,
                             sizeof (doSetProperty),
                             &doSetProperty
                             );

    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

    // Registers the audio route change listener callback function
    AudioSessionAddPropertyListener (
                                     kAudioSessionProperty_AudioRouteChange,
                                     audioRouteChangeListenerCallback,
                                     (__bridge void *)self
                                     );

// Activates the audio session.

    NSError *activationError = nil;
    [[AVAudioSession sharedInstance] setActive: YES error: &activationError];

    // Instantiates the AVAudioPlayer object, initializing it with the sound

    AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: medURL error: nil];
    self.appSoundPlayer = newPlayer;

    // "Preparing to play" attaches to the audio hardware and ensures that playback
    //      starts quickly when the user taps Play
    [appSoundPlayer prepareToPlay];
    [appSoundPlayer setVolume: 0.5];
    [appSoundPlayer setDelegate: self];
注意,我将类别设置为AVAudioSessionCategoryPlayback,这是之前建议的修复。自从我升级到iOS6后,这就不起作用了——事实证明,iOS5也不起作用

更新:我发现了一个基于此帖子的答案:


基本上,我进入了目标的信息页面,添加了一个新的键“Required background modes”,在这个键上我添加了一个字符串类型的值“App plays audio”。现在,在完全关闭屏幕之前,它会一直穿过轨道

根据这篇文章,我找到了一个似乎有效的答案:

基本上,我进入了目标的信息页面,添加了一个新的键“Required background modes”,在这个键上我添加了一个字符串类型的值“App plays audio”。现在,在完全关闭屏幕之前,它会一直穿过轨道