Audio Session
If your app is going to use a more sophisticated way of producing sound, such as an audio player (discussed in the next section), it must specify a policy regarding that sound. This policy will answer such questions as: should sound stop when the screen is locked? Should sound interrupt existing sound (being played, for example, by the Music app) or should it be layered on top of it?
Your policy is declared in an audio session, which is a singleton AVAudioSession instance created automatically as your app launches. You’ll need to link to AVFoundation.framework and import <AVFoundation/AVFoundation.h>
. You’ll refer to your app’s AVAudioSession by way of the class method sharedInstance
.
Note
Before iOS 6, it was also possible, and sometimes necessary, to talk to your audio session in C, by linking to AudioToolbox.framework and importing <AudioToolbox/AudioToolbox.h>
. In iOS 6, the C API isn’t needed, and I don’t use it in this edition of the book.
To declare your audio session’s policy, you’ll set its category, by calling setCategory:withOptions:error:
. The basic policies for audio playback are:
-
Ambient (
AVAudioSessionCategoryAmbient
) - Your app’s audio plays even while Music app music or other background audio is playing, and is silenced by the phone’s Silent switch and screen locking.
-
Solo Ambient (
AVAudioSessionCategorySoloAmbient
, the default) - Your app stops Music app music or other background audio from playing, and is silenced by the phone’s Silent switch and screen ...
Get Programming iOS 6, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.