An audio session is a Singleton object that you employ to set the audio context for your app and to express to the system your intentions for your app’s audio behavior.
- iOS 3.0+
- tvOS 3.0+
- watchOS 2.0+
Use this class to:
Activate or deactivate your app’s audio session
Set the audio session category and mode in order to communicate to the system how you intend to use audio in your app
Configure audio settings such as sample rate, I/O buffer duration, and number of channels
Handle audio route changes
React to important audio events, such as changes to the availability of the underlying Media Services daemon
Configuring the Audio Session
The shared audio session object acts as an intermediary between your app and the system’s media services daemon (and in turn, the underlying audio hardware). The media service also handles requests from other apps—some of which have a higher priority than your app’s audio needs, such as handling a phone call. Because of the intermediary role that the media service plays, your app does not directly control certain audio settings; instead, you request preferred values for these settings and then observe properties on the
AVAudioSession object (or related port and data source objects) to see whether, when, and to what extent your requests take effect.
For example, to set the number of output channels do the following:
setPreferredOutputNumberOfChannels(_:)with a proposed number of channels.
Even if your request was accepted, the number of channels you proposed might not be available, or the change might not take effect immediately.
If an audio route change occurs (due to such events as a phone call or audio in use by another app while yours is in the background), you should reapply your preferred audio settings, as needed, when your audio session reactivates. Subscribe to the
AVAudioSessionRouteChange notification to respond to these events.
Your requested changes might not take effect immediately. For example, you can use the
setPreferredDataSource(_:) method on the built-in microphone port to select the front microphone while a headset is plugged in—this preference does not take effect until the headset is unplugged and the audio route changes.
Routing and Mixing
Your audio session’s category (see Audio Session Categories) helps determine audio input and output behaviors when multiple sessions are active. Other active audio sessions can be associated with system services (such as the Music app, turn-by-turn navigation, or a phone call) or with background apps that support audio (see Background Execution).
To control how your audio session interacts with others, use the
setCategory(_:with:) method. If you choose an audio session category or option that supports mixing (see Audio Session Categories and
AVAudioSessionCategoryOptions), audio from other apps continues when you make your session active.
The system allows only one audio session to control audio routing. If multiple sessions are active, the system chooses which one controls routing based on priority and whether the sessions are mixable.
Activate Your Audio Session Only as Needed
For best customer experience, activate your audio session only as needed and deactivate it when you are not using audio. This is essential if you use a nonmixing audio session category.
Recording Requires User Permission
Recording audio requires explicit permission from the user. The first time your app’s audio session attempts to use an audio input route while using a category that enables recording, the system automatically prompts the user for permission. You can explicitly ask earlier by calling the
requestRecordPermission(_:) method. Until the user grants your app permission to record, your app can record only silence.
When a user responds to a recording permission prompt for your app, the system remembers the choice. If the user has denied recording permission, they can reenable it in Settings > Privacy > Microphone.
Media Services Availability
Under certain circumstances, the system terminates and restarts its media services daemon. Respond to these events by reinitializing your app’s audio objects (such as players, recorders, converters, or audio queues) and reasserting preferred audio session settings.
To be notified of media server restart, subscribe to the