Some favorite site feeds aggregated locally: iPhone Development RSS   Adobe Labs RSS   Macrumors RSS

Wednesday, July 29, 2009

iPhone: MPMusicPlayerController: currentPlaybackTime and playbackDuration

Wednesday, July 29, 2009   

I started playing around with the iPhone MPMusicPlayerController and found that getting and displaying a track's duration is pretty simple, displaying the current time of the track wasn't so easy. I was expecting to find a notification of some kind akin to IsPlaying or something like that so that I didn't need to fire my own NSTimer.

Well, there doesn't seem to be one, we have access to MPMusicPlayerControllerNowPlayingItemDidChangeNotification, MPMusicPlayerControllerPlaybackStateDidChangeNotification, and MPMusicPlayerControllerVolumeDidChangeNotification. How do you like those constants? Verbose to say the least.

Anyway, I found that I had to run my own Timer, and I just started it up within viewDidLoad - every 0.5 seconds, almost like an onEnterFrame in Flash. In the end my code looks something like this... this may save you some time. The labels are IBOutlet objects, etc.

- (void)onTimer:(NSTimer *)timer {
long currentPlaybackTime = self.musicPlayer.currentPlaybackTime;
int currentHours = (currentPlaybackTime / 3600);
int currentMinutes = ((currentPlaybackTime / 60) - currentHours*60);
int currentSeconds = (currentPlaybackTime % 60);
self.currentLabel.text = [NSString stringWithFormat:@"%i:%02d:%02d", currentHours, currentMinutes, currentSeconds];
}

- (void)viewDidLoad {
[super viewDidLoad];
self.musicPlayer = [MPMusicPlayerController iPodMusicPlayer];

// Initial sync of display with music player state
[self handleNowPlayingItemChanged:nil];
[self handlePlaybackStateChanged:nil];
[self handleExternalVolumeChanged:nil];

// Register for music player notifications
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self
selector:@selector(handleNowPlayingItemChanged:)
name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification
object:self.musicPlayer];
[notificationCenter addObserver:self
selector:@selector(handlePlaybackStateChanged:)
name:MPMusicPlayerControllerPlaybackStateDidChangeNotification
object:self.musicPlayer];
[notificationCenter addObserver:self
selector:@selector(handleExternalVolumeChanged:)
name:MPMusicPlayerControllerVolumeDidChangeNotification
object:self.musicPlayer];
[self.musicPlayer beginGeneratingPlaybackNotifications];
currentTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(onTimer:) userInfo:nil repeats:YES];
}

- (void)handleNowPlayingItemChanged:(id)notification {
MPMediaItem *currentItem = self.musicPlayer.nowPlayingItem;
self.songLabel.text = [currentItem valueForProperty:MPMediaItemPropertyTitle];
self.artistLabel.text = [currentItem valueForProperty:MPMediaItemPropertyArtist];
self.albumLabel.text = [currentItem valueForProperty:MPMediaItemPropertyAlbumTitle];

// Current Playback Value - via the running timer

// The total duration of the track...
long totalPlaybackTime = [[[musicPlayer nowPlayingItem] valueForProperty: @"playbackDuration"] longValue];
int tHours = (totalPlaybackTime / 3600);
int tMins = ((totalPlaybackTime/60) - tHours*60);
int tSecs = (totalPlaybackTime % 60 );
self.durationLabel.text = [NSString stringWithFormat:@"%i:%02d:%02d", tHours, tMins, tSecs ];

// Display album artwork. self.artworkImageView is a UIImageView.
CGSize artworkImageViewSize = self.artworkImageView.bounds.size;
MPMediaItemArtwork *artwork = [currentItem valueForProperty:MPMediaItemPropertyArtwork];
if (artwork != nil) {
self.artworkImageView.image = [artwork imageWithSize:artworkImageViewSize];
} else {
self.artworkImageView.image = nil;
}
}

Labels:

 
 Return to the main page
Comments:

There are currently 1 Comments:

Anonymous Anonymous said...
“Thanks alot! I have had a static time elapsed UILabel for about 5 hours until I found your blog.

Gary”
 
 Leave a comment