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.
Music players (instances of the
MPMusicPlayerControllerclass) always use a system-supplied audio session.
Movie players (instances of the
MPMoviePlayerControllerclass) use your application’s audio session by default, but can be configured to use a system-supplied audio session.
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:
Use the mixable category override property
kAudioSessionProperty_OverrideCategoryMixWithOthers, as described in “Fine-Tuning the Category,” to make an otherwise nonmixable playback category 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.
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.
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 Your Audio Session.”
Audio session configuration
Playing a movie silences all other audio
Movie and application audio mix, but other audio, including iPod, is silenced
All audio mixes
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:
Dispose of the movie player—even if you intend to play the same movie again later.
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.
© 2012 Apple Inc. All Rights Reserved. (Last updated: 2012-12-13)