Audio Session
AVAudioSession Class Reference object -c 接口
Each audio session category specifies a particular pattern of “yes” and “no” for each of the following behaviors, as detailed in “Audio Session Categories”:
-
Allows mixing:if yes, audio from other applications (such as the iPod) can continue playing when your application plays sound.
-
Silenced by the Silent switch and by screen locking: if yes, your audio is silenced when the user moves the Silent switch to silent and when the screen locks. (On iPhone, this switch is called the Ring/Silent switch.)
-
Supports audio input:if yes, application audio input, such as for recording, is allowed.
-
Supports audio output: if yes, application audio output, such as for playback, is allowed.
An audio session comes with some default behavior. Specifically:
-
Playback is enabled and recording is disabled.
-
When the user moves the Silent switch (or Ring/Silent switch on iPhone) to the “silent” position, your audio is silenced.
-
When the user presses the Sleep/Wake button to lock the screen, or when the Auto-Lock period expires, your audio is silenced.
-
When your audio starts, other audio on the device—such as iPod audio that was already playing—is silenced.
AVAudioSessionCategorySoloAmbient
audio session category—the default category.
iOS has six audio session categories:
-
Three for playback
-
One for recording
-
One that supports playback and recording—that need not occur simultaneously
-
One for offline audio processing
viewDidLoad
method—check the value of thekAudioSessionProperty_OtherAudioIsPlaying
property.The following categories allow use of hardware-assisted audio encoding and decoding:
-
AVAudioSessionCategorySoloAmbient
or the equivalentkAudioSessionCategory_SoloAmbientSound
-
AVAudioSessionCategoryPlayback
or the equivalentkAudioSessionCategory_MediaPlayback
-
AVAudioSessionCategoryPlayAndRecord
or the equivalentkAudioSessionCategory_PlayAndRecord
-
AVAudioSessionCategoryAudioProcessing
or the equivalentkAudioSessionCategory_AudioProcessing
Fine-Tuning the Category
You can fine-tune an audio session category in a variety of ways. Depending on the category, you can:
-
Allow other audio (such as from the iPod) to mix with yours when a category normally disallows it
-
Change the audio output route from the receiver to the speaker
-
Allow Bluetooth audio input
-
Specify that other audio should reduce in volume (“duck”) when your audio plays
Audio Interruption Handling Techniques
There are two alternative approaches for responding to interruptions:
-
ImplementObjective-C interruption delegate methodsprovided by the AV Foundation framework. In most cases, this approach is simpler and fits better with the rest of your application code.
-
Write a C-based interruption callback function as declared in Audio Session Services. This option requires you to register your callback with the audio session by using an explicit audio session initialization call.
The AVAudioRecorder
and AVAudioPlayback
classes provide their own delegate methods for responding to audio interruptions, as listed here:
-
audioPlayerBeginInterruption:
Called when an audio session is interrupted during playback. -
audioPlayerEndInterruption:
Called when a playback interruption ends. -
audioRecorderBeginInterruption:
Called when the audio session is interrupted during a recording. -
audioRecorderEndInterruption:
Called when a recording interruption ends.
Varieties of Audio Hardware Route Change
Handling audio hardware route changes
There are three parts to configuring your application to respond to route changes:
-
Implement methods to be invoked upon a route change.
-
Implement a property listener callback function that responds to route changes and uses the methods you implemented in step 1.
-
Register the callback function with the audio session.
Working with Movie Players
Desired behavior | Audio session configuration |
---|---|
Playing a movie silences all other audio |
|
Movie and application audio mix, but other audio, including iPod, is silenced |
|
All audio mixes |
|
Audio Session Cookbook
Checking if Other Audio is Playing During App Launch
UInt32 otherAudioIsPlaying; // 1 |
UInt32 propertySize = sizeof (otherAudioIsPlaying); |
|
AudioSessionGetProperty ( // 2 |
kAudioSessionProperty_OtherAudioIsPlaying, |
&propertySize, |
&otherAudioIsPlaying |
); |
|
if (otherAudioIsPlaying) { // 3 |
[[AVAudioSession sharedInstance] |
setCategory: AVAudioSessionCategoryAmbient |
error: nil]; |
} else { |
[[AVAudioSession sharedInstance] |
setCategory: AVAudioSessionCategorySoloAmbient |
error: nil]; |
} |
Modifying Playback Mixing Behavior
OSStatus propertySetError = 0; |
UInt32 allowMixing = true; |
|
propertySetError = AudioSessionSetProperty ( |
kAudioSessionProperty_OverrideCategoryMixWithOthers, // 1 |
sizeof (allowMixing), // 2 |
&allowMixing // 3 |
); |
Redirecting Output Audio
Overriding the output audio route
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker; // 1 |
|
AudioSessionSetProperty ( |
kAudioSessionProperty_OverrideAudioRoute, // 2 |
sizeof (audioRouteOverride), // 3 |
&audioRouteOverride // 4 |
); |
Supporting Bluetooth Audio Input
UInt32 allowBluetoothInput = 1; |
|
AudioSessionSetProperty ( |
kAudioSessionProperty_OverrideCategoryEnableBluetoothInput, |
sizeof (allowBluetoothInput), |
&allowBluetoothInput |
); |
Responding to Audio Session Interruptions
Implementing AVAudioSessionDelegate interruption methods - (void) beginInterruption { |
if (playing) { |
playing = NO; |
interruptedWhilePlaying = YES; |
[self updateUserInterface]; |
} |
} |
|
NSError *activationError = nil; |
- (void) endInterruption { |
if (interruptedWhilePlaying) { |
[[AVAudioSession sharedInstance] setActive: YES error: &activationError]; |
[player play]; |
playing = YES; |
interruptedWhilePlaying = NO; |
[self updateUserInterface]; |
} |
} |
Determining Whether a Device Supports Recording
BOOL inputAvailable; |
inputAvailable = [[AVAudioSession sharedInstance] inputIsAvailable]; |
Running Your App in the Simulator
#if TARGET_IPHONE_SIMULATOR |
#warning *** Simulator mode: audio session code works only on a device |
// Execute subset of code that works in the Simulator |
#else |
// Execute device-only code as well as the other code |
#endif |
Category identifiers* | Silenced by the Ring/Silent switch and by screen locking | Allows audio from other applications | Allows audio input (recording) and output (playback) |
---|---|---|---|
| Yes | Yes | Output only |
| Yes | No | Output only |
| No | No by default; yes by using override switch | Output only |
| No (recording continues with the screen locked) | No | Input only |
| No | No by default; yes by using override switch | Input and output |
| – | No | No input and no output |