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

时间:2012-12-16 18:30:03

标签: 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 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也无法正常工作!

更新:我根据这篇文章找到了一个似乎有效的答案:AVAudioPlayer stops playing on screen lock even though the category is AVAudioSessionCategoryPlayback

基本上我进入了目标的Info页面并添加了一个新的键“Required background modes”,为此我添加了一个字符串类型值“App播放音频”。现在,在完全关闭屏幕之前,它会完全通过轨道。

1 个答案:

答案 0 :(得分:1)

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

AVAudioPlayer stops playing on screen lock even though the category is AVAudioSessionCategoryPlayback

基本上我进入了目标的Info页面并添加了一个新的键“Required background modes”,为此我添加了一个字符串类型值“App播放音频”。现在,在完全关闭屏幕之前,它会完全通过轨道。