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





内附安装教程,含14套模板。 此源码为商业版全功能无授权版,1031全新的友价虚拟物品在线交易商城   模板源码,含14套模板带熊掌号及百度主动提交插件   源码运行环境,以下是友价虚拟物品在线交易   商城模板源码1031商业版的修复内容:   手机端:   新增手机版任务大厅功能   更新商家版会员中心界面   电脑端:   编辑商品视频栏目直接以弹窗方式展开,不用切换页面   调整保证金规则(有订单未完成,禁止解冻保证金)   新增商品问答功能   新增评价视频晒单功能   完善退款记录功能,每次退款跟处理结果都做记录(旧的只能保留最+新一次的退款记录)   支付宝等付款时,资金记录里同时记录交易号   阿里云OSS设置开关功能(后台基本设置-存储接口里)   开通阿里云OSS产品效果图也能存储的功能   友价   查看更多关于 友价 的文章   虚拟物品在线交易商城模板源码1023商业版源码安装教程   1.把程序上传到网站根目录下,不支持二级目录安装;   2.用EditPlus或者Notepad等代码编辑软件打开数据库配置文件:config/config.php 把里面的数据库信息改为自己的mysql数据库信息   文件内有标注,根据标注提示修改   3.导入数据库。把数据库目录下的shujku.sql数据库备份文件还原到你的mysql数据库内。   4.修改熊掌号地址,用editplus或者Notepad等代码编辑软件打开文件 user/baidu.php 把里面的这个域名 www.baidu.com 改为自己的   5.修改邮件通知名称,文件路径 yjadmin1/shop.php 把里面的关于 商业源码 这个名称,改为自己的,建议不要太长,否则会被屏蔽
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值