我对此代码有疑问:
-(void)playSong
{
// ...
NSLog(@"playing %i", currentSONG);
VKAudio* song = [audios objectAtIndex:currentSONG];
if (player == nil) { // create player object when first called
player = [[AVPlayer alloc] initWithURL:[self urlForVKAudio:song]];
[player play];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; // turn on in background
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleAVPlayerItemDidPlayToEndTimeNotification) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
}
else {
[player replaceCurrentItemWithPlayerItem:
[[AVPlayerItem alloc] initWithURL:[self urlForVKAudio:song]]];
[player play]; // <- this is not working when phone is blocked & screen is off
}
}
这里的问题是,当我在播放音乐时切换到bg模式时,它正在播放到当前项目的结尾,但是下一个项目没有播放,所以在后台模式方法
[player play];
什么都不做......我做错了什么? (bg音乐模式已开启)
答案 0 :(得分:1)
据我所知,您已正确设置AudioSession,但请勿激活它。
这就是我正在使用的:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];