Implementing the Music Player
Because the musicPlayer object was created in the viewDidLoad method of the view controller (see the start of "Implementing the Media Picker") and the music player's playlist was set in mediaPicker:didPickMediaItems:, the only real work that the playiPod: method must handle is starting and pausing playback.
To spice things up a bit, we'll try to be a bit clever-toggling the ipodPlayButton title between Play iPod Music (the default) and Pause iPod Music as needed. As a final touch, we'll access a property of musicPlayer MPMusicPlayerController object called nowPlayingItem. This property is an object of type MPMediaItem, which contains a string property called MPMediaItemPropertyTitle set to the name of the currently playing media file, if available.
To grab the title from musicPlayer.nowPlayingItem, we'll use a MPMediaItem instance method valueForProperty.
For example: [musicPlayer.nowPlayingItem valueForProperty:
MPMediaItemPropertyTitle]
If you attempt to use musicPlayer.nowPlayingItem. MPMediaItemPropertyTitle, it will fail. You must use the valueForProperty: method to retrieve the title or other MPMediaItem properties.
Putting this all together, we get the implementation of playiPod in Listing-11.
LISTING-11
1: -(IBAction)playiPod:(id)sender { 2: if ([ipodPlayButton.titleLabel.text isEqualToString:@"Play iPod Music"]) { 3: [musicPlayer play]; 4: [ipodPlayButton setTitle:@"Pause iPod Music" 5: forState:UIControlStateNormal]; 6: nowPlaying.text=[musicPlayer.nowPlayingItem 7: valueForProperty:MPMediaItemPropertyTitle]; 8: } else { 9: [musicPlayer pause]; 10: [ipodPlayButton setTitle:@"Play iPod Music" 11: forState:UIControlStateNormal]; 12: nowPlaying.text=@"No Song Playing"; 13: } 14: }
Line 2 checks to see whether the ipodPlayButton title is set to Play iPod Music. If it is, line 3 starts playback, lines 4-5 reset the button to read "Pause iPod Music", and lines 6-7 set the nowPlaying label to the title of the current audio track.
If the ipodPlayButton title is not Play iPod Music (line 8), the music is paused, the button title is reset to Play iPod Music, and the onscreen label is changed to display No Song Playing.
After completing the method implementation, save the MediaPlayground ViewController.m file, and choose Build and Run to test the application. Pressing the Choose iPod Music button will open a media picker.
After you've created a playlist, press the Done button in the media picker, and then touch Play iPod Music to begin playing the sounds you've chosen. The title of the current song is displayed at the bottom of the interface.
There was quite a bit covered in this lesson, but consider the capabilities you've uncovered. Your projects can now tie into the same media capabilities that Apple uses in its iPhone apps-delivering rich multimedia to your users with a relatively minimal amount of coding.
We touched on only a few of the configuration options available for the MPMoviePlayerController, MPMusicPlayerController, AVAudioPlayer, UIImagePickerController, and MPMediaPickerController classes-but far more customization is possible if you dig through the documentation.
The MPMoviePlayerController class, for example, offers the movieControlMode property for configuring the onscreen controls for when the movie is playing. You can also programmatically "scrub" through the movie by setting the playback starting point with the initialPlaybackTime property. As mentioned (but not demonstrated) in this lesson, this class can even play back a media file hosted on a remote URL-including streaming media.
Custom settings on AVAudioPlayer can help you create background sounds and music with properties such as numberOfLoops to set looping of the audio playback and volume for controlling volume dynamically. You can even enable and control advanced audio metering, monitoring the audio power in decibels for a given sound channel.
On the image side of things, the UIImagePickerController includes properties such as allowsEditing to enable the user to trim video clips or scale and crop images directly within the image picker. You'll want to check out the capability of this class to further control the iPhone's cameras (rear and front!) and record video.
As always, the Apple Xcode documentation utility provides an excellent place for exploring classes and finding associated sample code.