If you're using the AVPlayer
class and the primary purpose of your app is to play music, then you'll be able to run it in the background and thus update the nowPlayingInfo
when the track is changed.
Just a quick example:
- (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];
}
Unregister the remote control events in your dealloc
method:
[[UIApplication sharedApplication] endReceivingRemoteControlEvents]
Change Required Background Modes in your info.plist to App plays audio