Using Media Playback
To play songs, you use a music player—an instance of the MPMusicPlayerController class. In a nutshell, the steps to perform are as follows:
- Register to receive music player notifications, and turn on notifications
- Create a music player
- Set up a playback queue for the music player
- Configure the playback options
- Invoke playback
Registering for Music Player Notifications
If you provide music player playback control, or if you display information about the now-playing item, you must register for music player notifications
Listing 2-1 Registering for and activating music player notifications
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter
addObserver: self
selector: @selector (handle_NowPlayingItemChanged:)
name: MPMusicPlayerControllerNowPlayingItemDidChangeNotification
object: musicPlayer];
[notificationCenter
addObserver: self
selector: @selector (handle_PlaybackStateChanged:)
name: MPMusicPlayerControllerPlaybackStateDidChangeNotification
object: musicPlayer];
[musicPlayer beginGeneratingPlaybackNotifications];
Before deallocating a music player, unregister for notifications and then turn them off as shown in Listing 2-2.
Listing 2-2 Unregistering and deactivating music player notifications
[[NSNotificationCenter defaultCenter]
removeObserver: self
name: MPMusicPlayerControllerNowPlayingItemDidChangeNotification
object: musicPlayer];
[[NSNotificationCenter defaultCenter]
removeObserver: self
name: MPMusicPlayerControllerPlaybackStateDidChangeNotification
object: musicPlayer];
[musicPlayer endGeneratingPlaybackNotifications];
Creating and Configuring a Music Player
Listing 2-3 Creating an application music player
MPMusicPlayerController* appMusicPlayer =
[MPMusicPlayerController applicationMusicPlayer];
[appMusicPlayer setShuffleMode: MPMusicShuffleModeOff];
[appMusicPlayer setRepeatMode: MPMusicRepeatModeNone];
Listing 2-4 Creating an iPod music player
MPMusicPlayerController* iPodMusicPlayer =
[MPMusicPlayerController iPodMusicPlayer];
if ([iPodMusicPlayer nowPlayingItem]) {
// Update the UI (artwork, song name, volume indicator, etc.)
// to reflect the iPod state
}
Setting Up a Playback Queue
There are two main ways to set up a playback queue for a music player:
- Use a media item collection
- Use a media query, which implicitly defines a collection
You get a collection by using the database access classes or by employing the media item picker. Either way, applying the collection to a music player involves a single method call, shown here:
[musicPlayer setQueueWithItemCollection: userMediaItemCollection];
Alternatively, you can set up a queue using a media query—which, in turn, implicitly defines a collection, as shown here:
[musicPlayer setQueueWithQuery: [MPMediaQuery songsQuery]];
Listing 2-5 Setting up a playback queue in context
- (void) updateQueueWithCollection: (MPMediaItemCollection *) collection {
// Add 'collection' to the music player's playback queue, but only if
// the user chose at least one song to play.
if (collection) {
// If there's no playback queue yet...
if (userMediaItemCollection == nil) {
[self setUserMediaItemCollection: collection];
[musicPlayer setQueueWithItemCollection: userMediaItemCollection];
[musicPlayer play];
// Obtain the music player's state so it can be restored after
// updating the playback queue.
} else {
BOOL wasPlaying = NO;
if (musicPlayer.playbackState == MPMusicPlaybackStatePlaying) {
wasPlaying = YES;
}
// Save the now-playing item and its current playback time.
MPMediaItem *nowPlayingItem = musicPlayer.nowPlayingItem;
NSTimeInterval currentPlaybackTime = musicPlayer.currentPlaybackTime;
// Combine the previously-existing media item collection with
// the new one
NSMutableArray *combinedMediaItems =
[[userMediaItemCollection items] mutableCopy];
NSArray *newMediaItems = [mediaItemCollection items];
[combinedMediaItems addObjectsFromArray: newMediaItems];
[self setUserMediaItemCollection:
[MPMediaItemCollection collectionWithItems:
(NSArray *) combinedMediaItems]];
[musicPlayer setQueueWithItemCollection: userMediaItemCollection];
// Restore the now-playing item and its current playback time.
musicPlayer.nowPlayingItem = nowPlayingItem;
musicPlayer.currentPlaybackTime = currentPlaybackTime;
if (wasPlaying) {
[musicPlayer play];
}
}
}
}
Controlling Playback
In addition, the currentPlaybackTime property is read/write; you can use it to set the playback point within the now-playing item’s timeline.
using a slider to let the user set the playback point.
- (IBAction) setTimelinePosition: (id) sender {
[musicPlayer setCurrentPlaybackTime: [timelineSlider value]];
}