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

前端之家收集整理的这篇文章主要介绍了iOS5,iOS6为什么AVAudioPlayer播放停止在屏幕锁定?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我一直在为几个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播放音频”.现在,在完全关闭屏幕之前,它会完全通过轨道.

解决方法

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

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

基本上我进入了目标的Info页面添加了一个新的键“required background modes”,它会完全通过轨道.

猜你在找的iOS相关文章