如果您正在使用AVPlayer
该类并且您的应用程序的主要目的是播放音乐,那么您将能够在后台运行它,从而在nowPlayingInfo
更改曲目时更新。
只是一个简单的例子:
- (void)viewDidLoad {
[super viewDidLoad]
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
if ([[UIApplication sharedApplication] respondsToSelector:@selector(beginReceivingRemoteControlEvents)]){
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
//These two steps are important if you want the user to be able to change tracks with remote controls (you'll have to handle the remote control events yourself).
}
self.yourPlayer = [[AVPlayer alloc] init];
}
dealloc
在您的方法中取消注册远程控制事件:
[[UIApplication sharedApplication] endReceivingRemoteControlEvents]
将info.plist中的所需背景模式更改为App 播放音频