Music Player Integration

There is a music player framework for playing music/songs stored in iPhone, in your application. That will allow user to play all audio files that store in device.  iOS provide MediaPlayer framework to achive it. This will help user to choose the background music from their library in your game application. Application can also access number of songs stored, and other details of songs through this api.

There are two types of music players.

  1. The iPodMusicPlayer is a reference to the music player instance used by the iPod app. Any settings you change, such as the shuffle or repeat modes, will be changed in the iPod app, too. If the iPod is playing when your application starts, the music will continue playing and you can access the current song and skip back and forward through the currently active playlist. When your app quits, the music will continue playing. I imagine this mode is very handy for most utility apps that try to improve your music listening experience by interacting with the iPod.
  2. ApplicationMusicPlayer which gives a music player whose settings you can change independently of the iPod app.

The music player uses notifications to inform you about changes of:

  • current song (MPMusicPlayerControllerNowPlayingItemDidChangeNotification),
  • The play/paused/stopped state (MPMusicPlayerControllerPlaybackStateDidChangeNotification), or
  • the volume (MPMusicPlayerControllerVolumeDidChangeNotification).

Below steps will used to integrate media player in your application.
1.    Need to import Media framework (MediaPlayer.framework) unders frameworks in your application.

2.    Impoet header files in related class.
#import <MediaPlayer/MediaPlayer.h>

3.    Create object of  MPMusicPlayerController in related class,

@property (nonatomic, retain) MPMusicPlayerController *musicPlayer;

4.    Initialize object of  MPMusicPlayerController in .m file :

self.musicPlayer = [MPMusicPlayerController iPodMusicPlayer];

5.    Now register controller as an observer for the notifications,

// 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:MPMusicPlayerControllerPlaybackStateDidChangeNotificaion
object:self.musicPlayer];
[notificationCenter addObserver:self
selector:@selector(handleExternalVolumeChanged:)
name:MPMusicPlayerControllerVolumeDidChangeNotification
object:self.musicPlayer];
[self.musicPlayer beginGeneratingPlaybackNotifications];

6.    There is also a  notification that is sent by the iPod media library when the contents of the library change, e.g. when you sync your device with iTunes. Implement this notification if app creates its playlists that need to be updated after library changes.

[[MPMediaLibrary defaultMediaLibrary] beginGeneratingLibraryChangeNotifications]

7.    The notification handlers are where you update UI in response to changes in the player’s state:

// When the now playing item changes, update song display.
– (void)handleNowPlayingItemChanged:(id)notification
{
// Ask the music player for the current song.
MPMediaItem *currentItem = self.musicPlayer.nowPlayingItem;

self.lblSong.text   = [currentItem valueForProperty:MPMediaItemPropertyTitle];
self.lblArtist.text = [currentItem valueForProperty:MPMediaItemPropertyArtist];
self.lblAlbum.text  = [currentItem valueForProperty:MPMediaItemPropertyAlbumTitle];

// Display album artwork. self.artworkImageView is a UIImageView.
CGSize artworkImageViewSize = self.artworkImgview.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
– (void)handlePlaybackStateChanged:(id)notification
{
MPMusicPlaybackState playbackState = self.musicPlayer.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
– (void)handleExternalVolumeChanged:(id)notification
{
[self.volumeSlider setValue:self.musicPlayer.volume animated:YES];
}

MPMediaItem
A song is represented by an instance of the MPMediaItem class. Available properties include pretty much all the data that is available in iTunes: title, artist, album title and artist, genre, composer, duration, track/disc number, album artwork, rating, lyrics, last played date, and play and skip counts. The complete list of properties is available in the documentation.

MPMediaPickerController
Similar to the built-in image picker, Apple provides a complete user interface to select songs from the media library. All we need to do is create an instance of MPMediaPickerController, present it to the user as a modal view controller and implement the MPMediaPickerControllerDelegate protocol:

// MusicPlayerDemoViewController.h
@interface MusicPlayerDemoViewController : UIViewController <MPMediaPickerControllerDelegate>
{

}

// This action should open the media picker
– (IBAction)openMediaPicker:(id)sender;
@end

// MusicPlayerDemoViewController.m
– (IBAction)openMediaPicker:(id)sender
{
MPMediaPickerController *mediaPicker = [[MPMediaPickerController alloc]     initWithMediaTypes:MPMediaTypeMusic];
mediaPicker.delegate = self;
mediaPicker.allowsPickingMultipleItems = NO; // this is the default
[self presentModalViewController:mediaPicker animated:YES];
[mediaPicker release];
}

// Media picker delegate methods
– (void)mediaPicker: (MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection
{
// We need to dismiss the picker
[self dismissModalViewControllerAnimated:YES];

// Assign the selected item(s) to the music player and start playback.
[self.musicPlayer stop];
[self.musicPlayer setQueueWithItemCollection:mediaItemCollection];
[self.musicPlayer play];
}

– (void)mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker {
// User did not select anything
// We need to dismiss the picker
[self dismissModalViewControllerAnimated:YES];
}

MPMediaQuery
If you want to build a custom media picker UI or select songs programmatically without user interaction, you can do so by building queries with MPMediaQuery and MPMediaPropertyPredicate.

(Visited 5 times, 1 visits today)


Related Posts:

387
Author - admin_specindia

POPULAR BLOGS

Archives

  • 2017
  • 2016
  • 2015
  • 2014
  • 2013
  • 2012
  • 2011
  • 2010
  • 2009
  • 2008
  • Related Blogs

    Current Openings
    Scroll Up