Skip to content

Instantly share code, notes, and snippets.

@xslim
Created June 16, 2011 14:35
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xslim/1029343 to your computer and use it in GitHub Desktop.
Save xslim/1029343 to your computer and use it in GitHub Desktop.
MPMusicPlayerController snippets
- (void)setupiPodPlayer;
{
if (!self.ipodPlayer) {
self.ipodPlayer = [MPMusicPlayerController iPodMusicPlayer];
// Register for music player notifications
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self
selector:@selector(handleNowPlayingItemChanged:)
name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification
object:self.ipodPlayer];
[notificationCenter addObserver:self
selector:@selector(handlePlaybackStateChanged:)
name:MPMusicPlayerControllerPlaybackStateDidChangeNotification
object:self.ipodPlayer];
[notificationCenter addObserver:self
selector:@selector(handleExternalVolumeChanged:)
name:MPMusicPlayerControllerVolumeDidChangeNotification
object:self.ipodPlayer];
[self.ipodPlayer beginGeneratingPlaybackNotifications];
[[MPMediaLibrary defaultMediaLibrary] beginGeneratingLibraryChangeNotifications];
}
}
- (NSString *)currentSongPlaying
{
MPMediaItem *currentItem = self.ipodPlayer.nowPlayingItem;
NSString *songName = [NSString stringWithFormat:@"%@ - %@",
[currentItem valueForProperty:MPMediaItemPropertyArtist],
[currentItem valueForProperty:MPMediaItemPropertyTitle]];
return songName;
}
#pragma mark - Button actions
- (IBAction)playOrPauseMusic:(id)sender
{
MPMusicPlaybackState playbackState = self.ipodPlayer.playbackState;
if (playbackState == MPMusicPlaybackStateStopped || playbackState == MPMusicPlaybackStatePaused) {
[self.ipodPlayer play];
[[TKAlertCenter defaultCenter] postAlertWithMessage:[self currentSongPlaying]
image:[UIImage imageNamed:@"play"]];
} else if (playbackState == MPMusicPlaybackStatePlaying) {
[[TKAlertCenter defaultCenter] postAlertWithMessage:NSLocalizedString(@"Pause", nil)
image:[UIImage imageNamed:@"pause"]];
[self.ipodPlayer pause];
}
}
- (IBAction)playNextSong:(id)sender
{
[self.ipodPlayer skipToNextItem];
[[TKAlertCenter defaultCenter] postAlertWithMessage:[self currentSongPlaying]
image:[UIImage imageNamed:@"fastforward"]];
}
- (IBAction)playPreviousSong:(id)sender
{
static NSTimeInterval skipToBeginningOfSongIfElapsedTimeLongerThan = 3.5;
NSTimeInterval playbackTime = self.ipodPlayer.currentPlaybackTime;
if (playbackTime <= skipToBeginningOfSongIfElapsedTimeLongerThan) {
[self.ipodPlayer skipToPreviousItem];
} else {
[self.ipodPlayer skipToBeginning];
}
[[TKAlertCenter defaultCenter] postAlertWithMessage:[self currentSongPlaying]
image:[UIImage imageNamed:@"rewind"]];
}
- (IBAction)volumeSliderChanged:(id)sender
{
self.ipodPlayer.volume = self.volumeSlider.value;
}
#pragma mark - Swiping
- (void)volumeUp
{
float volume = self.ipodPlayer.volume;
if (volume == 1.0) return;
volume += 0.2;
if (volume > 1.0) volume = 1.0;
self.ipodPlayer.volume = volume;
}
- (void)volumeDown
{
float volume = self.ipodPlayer.volume;
if (volume == 0.0) return;
volume -= 0.2;
if (volume < 0.0) volume = 0.0;
self.ipodPlayer.volume = volume;
}
#pragma mark - Notifications
// When the now playing item changes, update song info labels and artwork display.
- (void)handleNowPlayingItemChanged:(id)notification
{
/*
// Ask the music player for the current song.
MPMediaItem *currentItem = self.ipodPlayer.nowPlayingItem;
// Display the artist, album, and song name for the now-playing media item.
// These are all UILabels.
self.songLabel.text = [currentItem valueForProperty:MPMediaItemPropertyTitle];
self.artistLabel.text = [currentItem valueForProperty:MPMediaItemPropertyArtist];
self.albumLabel.text = [currentItem valueForProperty:MPMediaItemPropertyAlbumTitle];
// 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;
}
*/
}
// When the playback state changes, set the play/pause button appropriately.
- (void)handlePlaybackStateChanged:(id)notification
{
/*
MPMusicPlaybackState playbackState = self.ipodPlayer.playbackState;
if (playbackState == MPMusicPlaybackStatePaused || playbackState == MPMusicPlaybackStateStopped) {
[self.playPauseButton setTitle:@"Play" forState:UIControlStateNormal];
} else if (playbackState == MPMusicPlaybackStatePlaying) {
[self.playPauseButton setTitle:@"Pause" forState:UIControlStateNormal];
}
*/
}
// When the volume changes, sync the volume slider
- (void)handleExternalVolumeChanged:(id)notification {
// self.volumeSlider is a UISlider used to display music volume.
// self.musicPlayer.volume ranges from 0.0 to 1.0.
//[self.volumeSlider setValue:self.musicPlayer.volume animated:YES];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment