If your app plays music, you may want to handle the remove audio control events. And you might also want to display applicable info like album art and the track name.
It’s not complicated, you mainly just need to know when to update it and what’s available to update. I use a method called updateNowPlaying.
- First make sure you’re dealing w/ iOS 5.
- Then create your image and trackname.
- Use those two items in an array and a matching array of key values (here’s a list of all the key options).
- Create a dictionary of the objects and keys.
- Set the now playing info (the dictionary from above) in the now playing info center.
-(void)updateNowPlaying;
{
/* make sure the have iOS 5 by checking for the applicable class. */
// Step 1: Check for iOS 5
if ([MPNowPlayingInfoCenter class])
{
// Step 2: image and track name
UIImage *musicImage = [UIImage imageNamed:@"logo.png"];
MPMediaItemArtwork *albumArt = [[MPMediaItemArtwork alloc]
initWithImage:musicImage];
NSString *trackName = [musicFiles objectAtIndex:curAudioFileIndex];
// Step 3: Create arrays
NSArray *objs = [NSArray arrayWithObjects:
trackName,
albumArt, nil];
NSArray *keys = [NSArray arrayWithObjects:
MPMediaItemPropertyTitle,
MPMediaItemPropertyArtwork, nil];
// Step 4: Create dictionary.
NSDictionary *currentTrackInfo = [NSDictionary dictionaryWithObjects:objs forKeys:keys];
// Step 5: Set now playing info
[MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = currentTrackInfo;
}
}