Home / iPhone Tips and Tutorials

Exploring Rich Media

We introduced you to System Sound Services for playing back short (30 second) sound files. This is great for alert sounds and similar applications but hardly taps the potential of the iPhone. This article takes things a bit further, giving you full playback capabilities, and even audio recording within your own applications.

In this article, we'll be using two new frameworks: Media Player and AV Foundation. These two frameworks encompass more than a dozen new classes. Although we won't be able to cover everything in this article, we'll give you a good idea of what's possible and how to get started.

In addition to these frameworks, we'll introduce the UIImagePickerController class. This simple object can be added to your applications to allow access to the iPhone's photo library or Camera from within your application.

Media Player Framework

The Media Player framework is used for playing back video and audio from either local or remote resources. It can be used to call up a modal iPod interface from your application, select songs, and manage playback. This is the framework that provides integration with all the built-in media features that your phone has to offer. We'll be making use of five different classes in our sample code:

  • MPMoviePlayerController: Allows playback of a piece of media, either located on the iPhone file system or through a remote URL. The player controller can provide a GUI for scrubbing through video, pausing, fast forwarding, or rewinding.
  • MPMediaPickerController: Presents the user with an interface for choosing media to play. You can filter the files displayed by the media picker or allow selection of any file from the media library.
  • MPMediaItem: A single piece of media, such as a song.
  • MPMediaItemCollection: Represents a collection of media items that will be used for playback. An instance of MPMediaPickerController returns an instance of MPMediaItemCollection that can be used directly with the next class-the music player controller.
  • MPMusicPlayerController: Handles the playback of media items and media item collections. Unlike the movie player controller, the music player works "behind the scenes"-allowing playback from anywhere in your application, regardless of what is displayed on the screen.

Of course, many dozens of methods are available in each of these classes. We'll be using a few simple features for starting and stopping playback, but there is an amazing amount of additional functionality that can be added to your applications with only a limited amount of coding involved.

Just as a reminder, a modal view is one that the user must interact with before a task can be completed. Modal views, such as the Media Player's iPod interface, are added on top of your existing views using a view's presentModalViewController method. They are dismissed with dismissModalViewControllerAnimated.

AV Foundation Framework

Although the Media Player framework is great for all your general media playback needs, Apple recommends the AV Foundation framework for most audio playback functions that exceed the 30 seconds allowed by System Sound Services. In addition, the AV Foundation framework offers audio recording features, making it possible to record new sound files directly in your application. This might sound like a complex programming task, but we'll do exactly that in our sample application.

You need just two new classes to add audio playback and recording to your apps:

  • AVAudioRecorder: Records audio (in a variety of different formats) to memory or a local file on the iPhone. The recording process can even continue while other functions are running in your application.
  • AVAudioPlayer: Plays back audio files of any length. Using this class, you can implement game soundtracks or other complex audio applications. You have complete control over the playback, including the ability to layer multiple sounds on top of one another.

The Image Picker

The Image Picker (UIImagePickerController) works similarly to the MPMediaPickerController, but instead of presenting a view where songs can be selected, the user's photo library is displayed instead. When the user chooses a photo, the image picker will hand us a UIImage object based on the user's selection.

Like the MPMediaPickerController, the image picker is presented within your application modally. The good news is that both of these objects implement their own view and view controller, so there's very little work that we need to do to get them to display other than a quick call to presentModalViewController.

As you can see, there's quite a few things to cover, so let's get started using these features in a real iPhone application.

[Contents] [Next]