AVsession Audio session

Audio Session


Audio Session Services   c语言接口

AVAudioSession Class Reference   object -c 接口

声音的协调者, 协调系统和应用之间的声音 处理。

根据不同的软件需求,设置不同的category

An  audio session  is the intermediary between your application and iOS for configuring audio behavior. Upon launch, your application automatically gets a  singleton  audio session. You configure it to express your application’s audio intentions.

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

AVAudioSessionCategoryAmbient

silenced by the Ring/Silent switch and when the screen locks.
audio from the iPod, Safari, and other built-in applications to play while your application is playing audio. 

AVAudioSessionCategorySoloAmbient    default category

 silenced when the user switches the Ring/Silent switch to the “silent” position and when the screen locks
silences other audio.

AVAudioSessionCategoryPlayback

Your audio plays even with the screen locked and with the Ring/Silent switch set to silent.

AVAudioSessionCategoryRecord

Use this category for recording audio. All playback on the phone—except for ringtones and Clock and Calendar alarms—is silenced.

AVAudioSessionCategoryPlayAndRecord
The input and output need not occur simultaneously, but can if needed. This is the category to use for audio chat applications.

AVAudioSessionCategoryAudioProcessing

Use this category when performing offline audio processing and not playing or recording.


 whether other audio is playing and then choose your category based on that. During launch—such as within the viewDidLoad method—check the value of thekAudioSessionProperty_OtherAudioIsPlaying property.

The following categories allow use of hardware-assisted audio encoding and decoding:

  • AVAudioSessionCategorySoloAmbient or the equivalent kAudioSessionCategory_SoloAmbientSound

  • AVAudioSessionCategoryPlayback or the equivalent kAudioSessionCategory_MediaPlayback

  • AVAudioSessionCategoryPlayAndRecord or the equivalent kAudioSessionCategory_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:

Varieties of Audio Hardware Route Change

Handling audio hardware route changes A flowchart representation of how Core Audio, and your property listener callback function, interact to provide good user experience upon an audio hardware route change.


There are three parts to configuring your application to respond to route changes:

  1. Implement methods to be invoked upon a route change.

  2. Implement a property listener callback function that responds to route changes and uses the methods you implemented in step 1.

  3. Register the callback function with the audio session.

Working with Movie Players

Configuring audio sessions when using a movie player

Desired behavior

Audio session configuration

Playing a movie silences all other audio

  • If your app does not itself play audio, do not configure an audio session.

  • If your app does play audio, configure an audio session and set its mixability according to whether or not you want to mix with iPod and other audio.

  • In either case, tell the movie player to use its own audio session:

    myMoviePlayer.useApplicationAudioSession = NO

Movie and application audio mix, but other audio, including iPod, is silenced

  • Configure an audio session using a nonmixable category.

  • Take advantage of the movie player’s defaultuseApplicationAudioSession value of YES

All audio mixes

  • Configure an audio session using a mixable category configuration.

  • Take advantage of the movie player’s defaultuseApplicationAudioSession value of YES

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

Audio session category behavior

Category identifiers*

Silenced by the Ring/Silent switch and by screen locking

Allows audio from other applications

Allows audio input (recording) and output (playback)

AVAudioSessionCategoryAmbient

kAudioSessionCategory_AmbientSound

Yes

Yes

Output only

AVAudioSessionCategorySoloAmbient

kAudioSessionCategory_SoloAmbientSound

Yes

No

Output only

AVAudioSessionCategoryPlayback

kAudioSessionCategory_MediaPlayback

No

No by default; yes by using override switch

Output only

AVAudioSessionCategoryRecord

kAudioSessionCategory_RecordAudio

No (recording continues with the screen locked)

No

Input only

AVAudioSessionCategoryPlayAndRecord

kAudioSessionCategory_PlayAndRecord

No

No by default; yes by using override switch

Input and output

AVAudioSessionCategoryAudioProcessing

kAudioSessionCategory_AudioProcessing

No

No input and no output





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值