Monday, May 12, 2014

AVAudioSession Interruption handling

An audio interruption is deactivation of an apps audio session which immediately pauses stops or pauses the audio. Interruptions happen a competing audio session from an app activates and that session is not categorized by the system to mix with the first app. 

Handling Audio interruptions by registering for appropriate NSNotification 

Below are the expected behavior from an apps 

After interruptions starts 
  - save state and context 
  - Update user interface 

After interruption ends 
  - restore state context 
  - Reactivated audio session if app appropriate 
  - Update user interface 

the above most of the behavior is automatically handled by the AVAudioPlayer or AVAudioRecroder objects. 

Depending on which audio technology is used, the interruption handling is interruptions 

AV Foundation Framework: The AVAudioPlayer and AVAudioRecorder classes provide delegate methods for interruption start and end. These methods can be implemented to update the ui and once the interruption ends, to resume paused playback. 

Audio Queue services, I/O audio unit: This technology makes the app to be in control of handling interruptions. The app is responsible for saving the playback or recording position and for re-activating the audio position after the interruption ends. 

open AL : When using openAL for audio playback, app needs to register for appropriate NSNotification notifications - as when using Audio Queue services. However, the delegate must additionally manage the audio queue context in this case. 

System sound services: Sounds played using System Sounds Services go silent when interruption begins. They can automatically be used again if the interruption ends. Apps cannot influence the interruption behavior for sounds that use this playback technology. 

Below is a better representation of how the OS handles the interruption  (Image courtesy: Apple)


If we are using AVAudioPlayer class, it provides it provides its own delegate methods via the AVAudioPlayerDelegate protocol. 

The delegate methods are audioPlayerBeginInterruption, audioPlayerEndInterruption.

If using the openAL for audio playback, application needs to implement and interruption listener callback function as we do with the Audio Queue services. However, the interruption handler must additionally manage the openAL context. upon receiving the interruption, set the context to null and after ending the interruption, set the context to the previous one. 




No comments:

Post a Comment