iOS Reference Library Apple Developer
Search

Working with Movies and iPod Music

Movie players let you play movies from a file or a network stream. Music players let you play audio content from a user’s iPod Library. To use these objects in coordination with your application audio, you must take into account their audio session characteristics.

Important: In iOS 3.1.3 and earlier, a movie player always uses a system-supplied audio session. To obtain that same behavior in iOS 3.2 and newer, you must set the movie player‚Äôs useApplicationAudioSession property value to NO.

Read “Working with Movie Players” for more information.

Working with Music Players

To play audio from a user’s iPod library along with your own sounds (as described in iPod Library Access Programming Guide), you must use a so-called mixable category configuration for your audio session. There are two, alternative ways to configure an audio session as mixable:

Having used one of these options, your sounds will not interrupt a music player—and neither will a music player’s sounds interrupt yours.

Important: Do not attempt to use a music player without configuring a mixable category for your audio session.

Because you must use a mixable category configuration, you don’t have access to hardware codecs for playback or recording. For details on this, see “How Categories Affect Encoding and Decoding.”

The system automatically handles route changes and interruptions for music players. You cannot influence this built-in behavior. As long as you correctly manage your application’s audio session as described here and in previous chapters, you can rely on a music player to take care of itself, as a user plugs in a headset, an alarm sounds, or a phone call arrives.

You can configure your audio session so that sound from a music player ducks (lowers in volume) when audio from your application plays. For details on ducking and how to enable it, see “Fine-Tuning the Category.”

For a description of the music player class, see MPMusicPlayerController Class Reference.

Working with Movie Players

By default, a movie player shares your application’s audio session. This means that, in effect, a movie player transcends the notion of mixing with your application’s audio; the movie player’s audio behaves as though it belongs to your application. No matter which playback category you choose, and no matter how you configure that category, your audio and the movie player’s audio never interrupt each other.

Sharing your audio session also gives you control over how a movie interacts with audio from other applications, such as the iPod. For example, if you set your category to AVAudioSessionCategoryAmbient and share your session, iPod audio is not interrupted when a movie starts in your app. Sharing your audio session also lets you specify whether or not movie audio obeys the Ring/Silent switch.

Important: In iOS 3.1.3 and earlier, a movie player always uses a system-supplied audio session. To obtain that same behavior in iOS 3.2 and newer, you must set the movie player‚Äôs useApplicationAudioSession property value to NO, as described in the first row of Table 6-1.

To configure audio behavior for a movie, determine the behavior you want and then perform appropriate audio session configuration—as described in Table 6-1. For details on setting up your audio session, see “Configuring the Audio Session.”

Table 6-1  Configuring audio sessions when using a movie player

Desired behavior

Audio session configuration

Playing a movie silences all other audio

  • If your app does not itself play audio, do not configure an audio session.

  • If your app does play audio, configure an audio session and set its mixability according to whether or not you want to mix with iPod and other audio.

  • In either case, tell the movie player to use its own audio session:

    myMoviePlayer.useApplicationAudioSession = NO

Movie and application audio mix, but other audio, including iPod, is silenced

  • Configure an audio session using a nonmixable category.

  • Take advantage of the movie player‚Äôs default useApplicationAudioSession value of YES

All audio mixes

  • Configure an audio session using a mixable category configuration.

  • Take advantage of the movie player‚Äôs default useApplicationAudioSession value of YES

Manage your application’s audio session as usual in terms of route changes and interruptions, as described in “Handling Audio Hardware Route Changes” and “Handling Audio Interruptions.” Enable ducking, if desired, as described in “Fine-Tuning the Category.”

If you have configured a movie player to use its own audio session, there’s some cleanup to perform. After a movie finishes, or the user dismisses it, do these two steps, in sequence, to regain the ability to play audio:

  1. Dispose of the movie player—even if you intend to play the same movie again later.

  2. Reactivate your audio session.

For a description of the movie player class, see MPMoviePlayerController Class Reference.

Using the Media Player Framework Exclusively

If your application is using a movie player only, or a music player only—and you are not playing your own sounds—then you should not configure an audio session.

If you are using a movie player exclusively, you must tell it to use its own audio session, as follows:

myMoviePlayer.useApplicationAudioSession = NO

If you are using a movie player and a music player, then you probably want to configure how the two interact; for this, you must configure an audio session, even though you are not playing application audio per se. Use the guidance in Table 6-1.




Last updated: 2010-07-09

Did this document help you? Yes It's good, but... Not helpful...