Playing music using the MediaPlayer framework

In this recipe we will learn how to play songs that are stored on the iOS device.

Getting ready

Following on from our previous recipe, create a new Single View Application, and name it MusicPlayerExample.

How to do it...

To begin, follow these simple steps as outlined in the following order:

  1. Add a reference to the MediaPlayer.framework to your project.
  2. Select the ViewController.xib files from the Project Navigator window.
  3. From the Object Library window, select and drag a UIToolbar object, and add this to our view and select the Item button located within our toolbar.
  4. From the Attributes Inspector section, change the Title property to read Browse.
  5. Create outlets for this UIBarButtonItem, and name it btnBrowse.
  6. Create the action method for the Browse button and name it btnBrowse.
  7. From the Object Library window, select and drag a UIBarButtonItem object, and add this next to the Browse button within our toolbar.
  8. From the Attributes Inspector section, change Title property to read Play.
  9. Create the outlets for this UIBarButtonItem, and name it btnPlay.
  10. Create the action method for the Play button and name it btnPlay.
  11. Our next step is to create the code functionality that will be responsible for playing music, when songs are selected and played from the Media Library.
  12. Open the ViewController.h interface file from the Project Navigator window.
  13. Next, create the following code sections as highlighted in the code snippet:
    //  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;
    
  14. Open the ViewController.m implementation file from the Project Navigator window, and enter the following highlighted code sections:
    @synthesize btnBrowse;
    @synthesize btnPlay;
    @synthesize mPicker;
    @synthesize mPlayer;
    
  15. Next, modify the 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];
    }
  16. Then, create the following code sections, as specified in the code snippet:
    #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];
    }
  17. Then, Build and Run the application by choosing Product | Run from the Product menu, or alternatively pressing Command + R.

When the compilation completes, click on the Browse button, and select one or more songs, then press the Play to play the chosen songs.

How it works...

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.

See also

  • The Playing video with Apple TV integration recipe
  • The Capturing media with the camera recipe
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset