【问题标题】:iOS5, iOS6 why does AVAudioPlayer playback stop on screen lock?iOS 5、iOS 6 为什么 AVAudioPlayer 播放在屏幕锁定时停止?
【发布时间】:2012-12-03 23:09:40
【问题描述】:

我一直在为几个 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

基本上我进入目标的信息页面并添加了一个新键“必需的背景模式”,为此我添加了一个字符串类型值“应用程序播放音频”。现在它在完全关闭屏幕之前一直穿过轨道。

【问题讨论】:

    标签: ios5 ios6 avaudioplayer avaudiosession


    【解决方案1】:

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

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

    基本上我进入目标的信息页面并添加了一个新键“必需的背景模式”,为此我添加了一个字符串类型值“应用程序播放音频”。现在它会在完全关闭屏幕之前一直穿过轨道。

    【讨论】:

      猜你喜欢
      • 2013-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-20
      • 2011-12-25
      相关资源
      最近更新 更多