Fine-Tuning an Audio Session for Players

Movie players let you play movies from a file or a network stream. Music players let you play audio content from a user’s music library. To use these objects in coordination with your app audio, consider their audio session characteristics.

Working with Music Players

To play audio from a user’s music library along with your own sounds (as described in iPod Library Access Programming Guide), you use the AVAudioSessionCategoryAmbient category, a so-called mixable category configuration for your audio session, or the AVAudioSessionCategoryOptionMixWithOthers option with a compatible category. Using this category ensures that your sounds will not interrupt a music player—nor will a music player’s sounds interrupt yours.

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 app’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 app plays. For details on ducking and how to enable it, see Fine-Tuning a Category.

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

Working with Movie Players

By default, a movie player shares your app’s audio session. In effect, a movie player transcends the notion of mixing with your app’s audio; the movie player’s audio behaves as though it belongs to your app. 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 apps, such as the Music app. For example, if you set your category to AVAudioSessionCategoryAmbient and share your session, Music 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 Defining an 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 play audio, do not configure an audio session.

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

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

    myMoviePlayer.useappAudioSession = NO

Movie and app audio mix, but other audio, including the Music app, is silenced

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 app’s audio session as usual in terms of route changes and interruptions, as described in Responding to Route Changes and Responding to Interruptions. Enable ducking, if desired, as described in Fine-Tuning a Category.

If you have configured a movie player to use its own audio session, there’s some cleanup to perform. To restore the movie player’s ability to play audio after a movie finishes, or the user dismisses it:

  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 app 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.useappAudioSession = 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 app audio per se. Use the guidance in Table 6-1.