In this recipe we will learn how to play songs that are stored on the iOS device.
Following on from our previous recipe, create a new Single View Application, and name it MusicPlayerExample.
To begin, follow these simple steps as outlined in the following order:
MediaPlayer.framework
to your project.ViewController.xib
files from the Project Navigator window.UIToolbar
object, and add this to our view and select the Item button located within our toolbar. UIBarButtonItem
, and name it btnBrowse
.action
method for the Browse button and name it btnBrowse
.UIBarButtonItem
object, and add this next to the Browse button within our toolbar. UIBarButtonItem
, and name it btnPlay
.action
method for the Play button and name it btnPlay
.ViewController.h
interface file from the Project Navigator window.// ViewController.h // MusicPlayerExample // Created by Steven F Daniel on 20/11/12. // Copyright (c) 2012 GenieSoft Studios. All rights reserved. #import <UIKit/UIKit.h> #import <MediaPlayer/MediaPlayer.h> @interface ViewController : UIViewController<MPMediaPickerControllerDelegate> { } // Declare the Getters and Setters for each of our objects. @property (nonatomic, retain) MPMusicPlayerController *mPlayer; @property (nonatomic, retain) MPMediaPickerController *mPicker;
ViewController.m
implementation file from the Project Navigator window, and enter the following highlighted code sections:@synthesize btnBrowse; @synthesize btnPlay; @synthesize mPicker; @synthesize mPlayer;
viewDidLoad:
method as shown in the code snippet: - (void)viewDidLoad { [super viewDidLoad]; // Create the Media Picker and Music Player controller's self.mPicker = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeMusic]; self.mPlayer = [[MPMusicPlayerController alloc] init]; [mPlayer prepareToPlay]; self.view.backgroundColor = [UIColor blackColor]; }
#pragma mark called when the user presses the Browse button - (IBAction)btnBrowse:(id)sender { self.mPicker.delegate = self; self.mPicker.allowsPickingMultipleItems = YES; [self presentViewController:mPicker animated:YES completion:nil]; } -(IBAction)btnPlay:(id)sender { // Check to see if we are already playing our audio. if (!self.mPlayer.playbackState == MPMusicPlaybackStatePlaying) { [self.btnPlay setTitle :@"Stop"]; [self.mPlayer play]; } else { [self.btnPlay setTitle:@"Play"]; [self.mPlayer stop]; } } #pragma mark - Image Picker Delegate Methods -(void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection{ [self.mPlayer setQueueWithItemCollection:mediaItemCollection]; [self.mPicker dismissViewControllerAnimated:YES completion:nil]; } #pragma mark called when the user cancels the media picker -(void)mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker { [self.mPicker dismissViewControllerAnimated:YES completion:nil]; }
When the compilation completes, click on the Browse button, and select one or more songs, then press the Play to play the chosen songs.
In this recipe, we start by extending our class, to include each of the following class protocols: MPMediaPickerControllerDelegate
so that we can access each of their respective properties and methods. We then initialize the media picker and pass the type of media we want it to look for and then initialize and create an instance of the MPMusicPlayerController
class.
Next, we declare our btnBrowse
method that will display the iOS device music library interface, and allows for multiple files to be selected. In our btnPlay:
method, we check to see if any songs are currently being played, which is determined by checking the playbackState
property. Playing and stopping of songs is achieved through the play
and stop
methods of the MPMusicPlayerController
class object. In the mediaPicker:didPickMediaItems:
method, we set the songs that were picked by the user to the music player, through the use of the setQueueWithItemCollection:
method and dismiss the modal media picker controller.