iOS audio session AVAudioSession
AVAudioSession means:
An intermediary object that communicates to the system how you intend to use audio in your app
acts as an intermediary, telling the system how audio is used in your app
All iOS and tvOS apps have audio sessions and come pre-configured as follows:
- Audio playback is activated, but audio recording is not (audio recording is not supported in tvOS)
- All audio played by the app disappears when the user toggles the ring/silent switch to silent mode
- Audio is muted when device is locked
- When the app plays audio, all other audio playing in the background is muted
If you want to change the default behavior, you can configure the app's audio session classification
There are about 7 audio session categories, refer to Audio Session Categories
AVAudioSessionCategoryAmbient
- This category means that the application playing sound is not primary, i.e. your application can be used successfully with the sound offAVAudioSessionCategorySoloAmbient
- Default audio session classificationAVAudioSessionCategoryPlayback
- Used to play recorded music or other soundsAVAudioSessionCategoryRecord
- Voice recorder, audio captureAVAudioSessionCategoryPlayAndRecord
- Record and play audio, e.g.VoIP
applicationsAVAudioSessionCategoryMultiRoute
- Class for routing different audio streams to different output devices simultaneouslyAVAudioSessionCategoryAudioProcessing
- Offline sessions and handling, deprecated in iOS10
Usually the most used is AVAudioSessionCategoryPlayback
that after specifying this category, the audio can continue to be played when the user switches the ring/silent switch to silent mode (iOS only). Use this category:
your app can also play background audio if you’re using the Audio, AirPlay, and Picture in Picture background mode. For more information, see Enabling Background Audio.
Used AVAudioSession
to configure the application's audio session, AVAudioSession
is a singleton used to set the audio session category and perform other configuration. You can interact with the audio session throughout the life of the application, but it is often useful to perform this configuration at application startup, as shown in the following example:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]?) -> Bool {
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(AVAudioSessionCategoryPlayback)
} catch {
print("Setting category to AVAudioSessionCategoryPlayback failed.")
}
// Other project setup
return true
}
This category will be used when you activate an audio session using the setActive(_:)
or methodsetActive(_:with:)
Notice:
You can activate the audio session at any time after setting its category, but it's generally preferable to defer this call until your app begins audio playback. Deferring the call ensures that you won't prematurely interrupt any other background audio that may be in progress.
You can activate this audio session at any time after setting the classification, but it is usually best to delay it until the application starts audio playback. Postponing the call ensures that you don't prematurely interrupt any other background audio that may be in progress