9.5. Playing Audio Over Other Active Sounds
Problem
You either want to put other applications in silent mode while you play audio or play audio on top of other applications’ audio playback (if any).
Solution
Use audio sessions to set the type of audio category your application uses.
Discussion
The AVAudioSession class
was introduced in the AV Foundation framework. Every iOS application has one audio session.
This audio session can be accessed using the sharedInstance class method of the AVAudioSession class, like so:
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
After retrieving an instance of the AVAudioSession class, you can invoke the
setCategory:error:
instance method of the audio session object to choose among the
different categories available to iOS applications. Different values
that can be set as the audio session category of an application are
listed here:
AVAudioSessionCategoryAmbientThis category will not stop the audio from other applications, but it will allow you to play audio over the audio being played by other applications, such as the Music app. The main UI thread of your application will function normally. The
prepareToPlayandplayinstance methods ofAVAudioPlayerwill return with the valueYES. The audio being played by your application will stop when the user locks the screen. The silent mode silences the audio playback of your application only if your application is the only application playing an audio file. If you start playing audio while the ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access