Media Playback
Let’s first look at playing back existing media stored in the iPod library. Apple has provided convenience classes that allow you to select and play back iPod media inside your own application as part of the Media Player framework.
Warning
The following examples make use of the iPod library; this is not present in the iPhone Simulator and will only work correctly on the device itself.
The approach uses picker controllers and delegates as in the
previous chapter. In this example I use an MPMediaPickerController that, via the MPMediaPickerControllerDelegate protocol,
returns an MPMediaItemCollection object
containing the media items the user has selected. The collection of items
can be played using an MPMusicPlayerController object.
Lets go ahead and build a simple media player application to illustrate how to use the media picker controller. Open Xcode and start a new View-based Application project, naming it “Audio” when requested. Click on the Audio project file in the Project navigator window, select the Target and click on the Build Phases tab. Click on the Link with Libraries drop down and click on the + button to add the MediaPlayer framework.
Edit the AudioViewController.h
interface file to import the MediaPlayer framework and declare the class
as an MPMediaPickerControllerDelegate.
Then add the IBOutlet instance
variables and IBAction methods for the
buttons we will create in Interface Builder:
#import <UIKit/UIKit.h> #import <MediaPlayer/MediaPlayer.h> @interface ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access