2012-12-16 42 views
4

我一直在为几个iOS应用程序使用AVAudioPlayer,它们都在屏幕锁定之前成功回放,直到我的iPad2上的iOS6(或iOS5),现在它们停止。这是我如何设置它,它用来做工精细:iOS5,iOS6为什么AVAudioPlayer播放在屏幕锁定时停止?

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

基本上我走进了目标的信息页,并增加了新的密钥“所需的背景模式”,这个我加字符串类型值“应用程序播放音频”。现在,它可以在完全关闭屏幕之前完成整个轨道。

回答

相关问题