iphone iOS5 audioPlayerDidFinishPlaying未调用屏幕锁定



我有一系列音频文件,我想按顺序播放。我设置audioPlayerDidFinishPlaying委托方法来启动序列中的下一个文件。如果我关闭屏幕锁定或在模拟器或iOS4设备上运行它,这不会有问题。然而,当我在一个断开连接的设备上运行它时,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
                         );
// Registers the audio route change listener callback function
AudioSessionAddPropertyListener (
                                 kAudioSessionProperty_AudioRouteChange,
                                 audioRouteChangeListenerCallback,
                                 self
                                 );
// Activates the audio session.
NSError *activationError = nil;
[[AVAudioSession sharedInstance] setActive: YES error: &activationError];
    NSString *path = [[NSBundle mainBundle] pathForResource:filename ofType:@"m4a"];  
    float vol = [appSoundPlayer volume];
    AVAudioPlayer * newAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];  
    self.appSoundPlayer = newAudio; // automatically retain audio and dealloc old file if new file is loaded
    [newAudio release]; // release the audio safely
    appSoundPlayer.delegate = self; 
    [appSoundPlayer prepareToPlay];
    [appSoundPlayer setNumberOfLoops:0];
    [appSoundPlayer play];
    [appSoundPlayer setVolume:vol];
    [progTime setProgress:0];
}

   - (void) audioPlayerDidFinishPlaying: (AVAudioPlayer *) lappSoundPlayer successfully: (BOOL) flag {
    //... code here to select nex sound track
                    [lappSoundPlayer play];
    }

我已经这样做了,所以应用程序继续播放当前曲目,即使屏幕锁定,我只是不能让它开始下一个曲目。

注意:我已经验证了这在iOS4中工作

您是否设置了音频会话类别,特别是AVAudioSessionCategoryPlayback,如这里所述?

[[AVAudioSession sharedInstance] setDelegate: self];    
NSError *error = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&error];

如果这不起作用,另一件事可以尝试,根据用户ascanio在苹果开发论坛的这条消息,是设置你的应用程序监听远程控制事件:

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

最新更新