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 MPMusicPlayerController
class) always use a system-supplied audio session.
Movie players (instances of the MPMoviePlayerController
class) use your application’s audio session by default, but can be configured to use a system-supplied audio session.
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.
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 AVAudioSessionCategoryAmbient
(or the equivalent kAudioSessionCategory_AmbientSound
) category—which is always 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.
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.
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.”
Desired behavior | 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.
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