Online Book Reader

Home Category

iOS Recipes - Matt Drance [26]

By Root 273 0
then pass the MPMediaItemCollection object, which contains the tracks of the album, back to the calling code.

InfinitePlayback/PRPTileView.m

- (MPMediaItemCollection *)collectionFromTouch:(CGPoint)touchPoint {

int col = touchPoint.x / SIZE;

int row = touchPoint.y / SIZE;

int position = row*columns+col;

int index = position%albums;

MPMediaItemCollection *mCollection = [self.albumCollections

objectAtIndex:index];

return mCollection;

}

In the viewDidLoad method of our new PRPMusicViewController class we need to create an instance of the MPMusicPlayerController, the object that allows us to control playback of music from the iPod library. We also need to register for the iPod music player notifications, which are essential to allow us to respond to changes made by the MPMusicPlayerController itself, such as playing the next track in a playlist or stopping play at the end of an album. In those cases, we want to respond by changing the track information in the display or switching the playback button image.

InfinitePlayback/PRPMusicViewController.m

- (void)viewDidLoad

{

[super viewDidLoad];

myPlayer = [[MPMusicPlayerController applicationMusicPlayer] retain];

NSNotificationCenter *notificationCenter = [NSNotificationCenter

defaultCenter];

[notificationCenter

addObserver: self

selector: @selector (playingItemChanged:)

name: MPMusicPlayerControllerNowPlayingItemDidChangeNotification

object: myPlayer];

[notificationCenter

addObserver: self

selector: @selector (playbackStateChanged:)

name: MPMusicPlayerControllerPlaybackStateDidChangeNotification

object: myPlayer];

[myPlayer beginGeneratingPlaybackNotifications];

}

In the viewWillAppear: method, we need to extract the album art again and set it as the background for our controller. In the XIB file for this view controller we have a small view hierarchy that places the album art under a semitransparent image of the classic CD case, with the playback controls added on top. We need to set the playback queue to be the full list of tracks for this album, and, in this simple case, we automatically play the first track.

InfinitePlayback/PRPMusicViewController.m

- (void) viewWillAppear:(BOOL)animated {

[super viewWillAppear:animated];

MPMediaItem *mItem = [self.mCollection representativeItem];

MPMediaItemArtwork *artwork =

[mItem valueForProperty: MPMediaItemPropertyArtwork];

UIImage *image = [artwork imageWithSize: CGSizeMake(280, 280)];

if (!image) image = [UIImage imageNamed:@"missing.png"];

self.albumCover.image = image;

[myPlayer setQueueWithItemCollection: self.mCollection];

[myPlayer play];

}

To allow the Play button to work as a Play/Pause toggle, we can test the playBackState property of the music controller to see whether the music is playing or not and adjust the playback state accordingly. Because we have registered for playbackStateChanged: notifications, there’s no need to make any changes to the playback button image; this is taken care of by the code handling the notification.

InfinitePlayback/PRPMusicViewController.m

- (IBAction)playButton:(UIButton *)sender {

if (myPlayer.playbackState == MPMoviePlaybackStatePlaying) {

[myPlayer pause];

} else {

[myPlayer play];

}

}

When a notification is triggered, it calls the method we selected. The playingItemChanged: call allows us to update the track information in the display. We can use the nowPlayingItem method to fetch the data for the currently playing track and use the valueForProperty: method of the MPMediaItem class to retrieve text for each item that we need. We can also check to see whether the playBackState has changed and adjust the displayed text accordingly.

InfinitePlayback/PRPMusicViewController.m

- (void)playingItemChanged: (id) notification {

MPMediaItem *currentItem = [myPlayer nowPlayingItem];

albumName.text = [currentItem valueForProperty:

MPMediaItemPropertyAlbumTitle];

trackName.text = [currentItem valueForProperty:

MPMediaItemPropertyTitle];

if (myPlayer.playbackState == MPMusicPlaybackStateStopped) {

trackName.text = @"PlayBack Complete";

Return Main Page Previous Page Next Page

®Online Book Reader