Sunday, May 11, 2014

AVAudioSession A high level look

AudioSession is an intermediary between an iOS app and iOS for configuring audio behavior. Upon launch an app automatically gets a singleton audio session. an app can configure this default behavior for the needs of an app's audio behavior. Some of the practical examples for configuring the default audio session could be for these below

- Mix apps sound with those from other apps such as music app or if the app intends to silence other audio
- Responding to an audio interruption such as incoming call or clock Alarm
- Respoding when a user plugs in the headset or unplug the headset

the default behavior of Audio session is like below

- Playback is enabled and recording is disabled
- When user moves the slient switch (or ring/silent switch on iPhone) to silent position, the audio is silenced
- When the user presses the Wake/Sleep button on the lock button, or when the lock period expires, the app audio is silenced
- When the app audio starts, other audio on the device stops.

the main audio session categories are:

AVAudioSessionCategoryAmbient : This category is used for apps which are like play along apps. Such a virtual piano apps which plays along with music apps audio that goes on in background. Such app's audio will be muted when silent switch is turned to silent and by screen getting locked.

AVAudioSessionCategorySoloAmbient:  This is the default category. This category doesnt allow the audio mixing with the background audio. when the apps session starts playing auido, other app's audio will get interrupted. Similar to the previous category, the audio gets stopped when the wake lock timer expires or wake/lock button is pressed. And also get silent when the ringer/silent button is set to silent.

AVAudioSessionCategoryPlayback: This category is used for playing recorded sounds. Using this category will let the app to continue playing the audio even after the wake/lock button pressed or ringer/silent button is set to silent. Inorder for app to continue playing the audio when transition to background, apps audio property must be set to UIBackgroundModes

AVAudioSessionCategoryRecord: This category is used for recording audio. this category silences audio. To continue recording even when app transitions to background, we need to add the audio value UIBackgroundModes to the plist file.

AVAudioSessionCategoryPlayAndRecord: This category is most useful for VOIP type of apps. The audio continues when even when the ring/silent switch is set to silent. Like other categories to continue record and play in background, audio property value must be set to UIBackgroundModes. This category by default is non mixable. however, we can set the property so that it becomes mixable. The property is AVAudioSessionCategoryMixWithOthers

 AVAudioSessionCategoryAudioProcessing: This category is used for using audio hardware codec or signal processor while not playing or recording audio. This category by default disables the audio playback and recording. The audio processing cannot continue in background. however, when app is pushed to background, an app can request for additional processing time.

AVAudioSessionCategoryMultiRoute: This category is used for routing distinct audio streams to different devices at the same time. For e.g. this category can be used to route audio to both a USB device and a headset.

No comments:

Post a Comment