Playing music in your app - MediaPlayer tutorial
In this tutorial we will have a look at how to use MediaPlayer framework to play music from your app. To begin with we need to#import <MediaPlayer/MediaPlayer.h>. There are three core classes that we will work with.
MPMusicPlayerController There are two types of MPMusicPlayerController
Application music player ([MPMusicPlayerController applicationMusicPlayer])Â that plays music just in your app and does not affect the in-built iPod
iPod music player ([MPMusicPlayerController iPodMusicPlayer])Â that employs the in-built iPod to play music - the music can be controlled in the iPod app and remains playing even after you quit your app
MPMusicPlayerController methods are quite self-explanatory:
- setQueueWithQuery: and - setQueueWithItemCollection are used to set playback queue with query (see below)
- play, - pause, - stop, - beginSeekingForward, - beginSeekingBackward, - endSeeking, - skipToNextItem, - skipToPreviousItem, - skipToBeginning are all used to control the player
currentPlaybackTime, nowPlayingItem, playbackState, repeatMode, shuffleMode, volume are all properties of the player that you can use to change the behavior or display different attributes
MPMediaItem MPMediaItem is a single item in the media library (such as one song). It is the nowPlayingItem property of the player. Each item has metadata associated with it, those can be accessed by calling valueForProperty: method. The full list of properties can be found in thedocumentation but some examples include MPMediaItemPropertyTitle or MPMediaItemPropertyArtist.
MPMediaQuery MPMediaQuery is used to do queries in the iPod library. Again, the full description is best to be found in the documentation but a little example demonstrates how MPMediaQuery works.
MPMediaQuery* query = [MPMediaQuery songsQuery]; [query addFilterPredicate:[MPMediaPropertyPredicate predicateWithValue:@"The Beatles" forProperty:MPMediaItemPropertyArtist comparisonType:MPMediaPredicateComparisonEqualTo]]; [query setGroupingType:MPMediaGroupingAlbum];
This creates a query that selects all songs whose artist is "The Beatles" and groups them by album. All the songs in the query can be passed to MPMusicPlayerController to play.
Example Now put it all together:
//Create an instance of MPMusicPlayerController MPMusicPlayerController* myPlayer = [MPMusicPlayerController iPodMusicPlayer]; //Create a query that will return all songs by The Beatles grouped by album MPMediaQuery* query = [MPMediaQuery songsQuery]; [query addFilterPredicate:[MPMediaPropertyPredicate predicateWithValue:@"The Beatles" forProperty:MPMediaItemPropertyArtist comparisonType:MPMediaPredicateComparisonEqualTo]]; [query setGroupingType:MPMediaGroupingAlbum]; //Pass the query to the player [myPlayer setQueueWithQuery:query]; //Start playing and set a label text to the name and image to the cover art of the song that is playing [myPlayer play]; someLabel.text = [myPlayer.nowPlayingItem valueForProperty:MPMediaItemPropertyTitle]; someImageView.image = [myPlayer.nowPlayingItem valueForProperty:MPMediaItemPropertyArtwork];Â













