iOS - AVAudioSession

AVAudioSession可以指定你的应用程序需要的音频行为,比如是播放还是录音,是否支持蓝牙,是否支持后台播放,与其他应用的音频是混合播放还是独占播放等等,下面讲对这些进行介绍。

一、初始化

AVAudioSession类由AVFoundation框架引入,AVAudioSession是一个单例模式,每个iOS应用都有一个音频会话,AVAudioSession的初始化方法为:

#import 

AVAudioSession *audioSession = [AVAudioSession sharedInstance];

二、激活和关闭

首先在使用音频会话AVAudioSession前,我们需要激活它,代码如下:

    // 激活
    [audioSession setActive:YES error:NULL];
    // 音频会话使用完毕后记得关闭
    [audioSession setActive:NO error:NULL];

另外一个方法还可以设置选项参数AVAudioSessionSetActiveOptions

    /*  options for use when calling setActive:withOptions:error:
     AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation --
     Notify an interrupted app that the interruption has ended and it may resume playback. Only valid on
     session deactivation.
    typedef NS_OPTIONS(NSUInteger, AVAudioSessionSetActiveOptions)
    {
        AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation = 1
    } NS_AVAILABLE_IOS(6_0);
    */
    
    // 此处只有 AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation 这一个参数
    [audioSession setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:NULL];

三、设置

设置音频会话AVAudioSession的代码如下:

- (BOOL)setCategory:(NSString *)category withOptions:(AVAudioSessionCategoryOptions)options error:(NSError **)outError

从上面代码可以知道,设置音频会话AVAudioSession有两个参数:category(模式)和options(选项),我们先介绍category

Category Meaning Behavior background silence switch
AVAudioSessionCategory- SoloAmbient Sound isn’t essential, but it silences other audio. For example, a game with a soundtrack. / 播放类型。 Doesn’t mix with other sounds. / 独占播放,其他音乐会停止播放。 Doesn’t play in the background. / 不支持(后台播放) Responds to the silence switch. / 遵从(静音键和屏幕关闭)
AVAudioSessionCategory- Ambient Sound isn’t essential, and it doesn’t silence other audio. For example, a game that lets people play music from another app during gameplay in place of the game’s soundtrack. / 播放类型。 Mixes with other sounds. / 混合播放,可以和其他音频应用同时播放。 Doesn’t play in the background. / 不支持 Responds to the silence switch. / 遵从
AVAudioSessionCategory- Playback Sound is essential and might mix with other audio. For example, an audiobook or educational app that teaches a foreign language, which people might want to listen to after leaving the app. / 播放类型,用于以语音为主的应用 May or may not mix with other sounds. / 可独占可混合。 Can play in the background. / 支持 Doesn’t respond to the silence switch. / 不遵从
AVAudioSessionCategory- Record Sound is recorded. For example, a note-taking app that offers an audio recording mode. An app of this nature might switch its category to playback if it lets people play the recorded notes. / 录音类型 Doesn’t mix with other sounds. / 独占。 Can record in the background. / 支持 Does’t respond to the silence switch. / 不遵从
AVAudioSessionCategory- PlayAndRecord Sound is recorded and played, potentially simultaneously. For example, an audio messaging or video calling app. / 播放和录音类型 May or may not mix with other sounds. / 可独占可混合。 Can record and play in the background. / 支持 Doesn’t respond to the silence switch. / 不遵从
AVAudioSessionCategory- AudioProcessing 音频处理 不能播放和录制任何声音。 / 不遵从
AVAudioSessionCategory- MultiRoute 多种输入输出 / / 不遵从

再看options,此处的options有五个:

typedef NS_OPTIONS(NSUInteger, AVAudioSessionCategoryOptions)
    {
        AVAudioSessionCategoryOptionMixWithOthers   = 0x1,
        AVAudioSessionCategoryOptionDuckOthers    = 0x2,
        AVAudioSessionCategoryOptionAllowBluetooth __TVOS_PROHIBITED  = 0x4,
        AVAudioSessionCategoryOptionDefaultToSpeaker __TVOS_PROHIBITED  = 0x8,
        AVAudioSessionCategoryOptionInterruptSpokenAudioAndMixWithOthers NS_AVAILABLE_IOS(9_0)  = 0x11
    } NS_AVAILABLE_IOS(6_0);
AVAudioSessionCategoryOptionMixWithOthers -

1.有效的只有音频会话类是AVAudioSessionCategoryPlayAndRecord, AVAudioSessionCategoryPlayback, 和 AVAudioSessionCategoryMultiRoute
2.如果你使用这个选项激活你的会话,你的应用程序的音频不会中断从其他应用程序(如音乐应用程序)的音频。如果不使用此选项,激活你的会话会打断其他nonmixable会话。

AVAudioSessionCategoryOptionDuckOthers -

1.有效的只有音频会话类是AVAudioSessionCategoryPlayAndRecord, AVAudioSessionCategoryPlayback, 和 AVAudioSessionCategoryMultiRoute
2.如果你想在你的应用程序运行时,仍能听到其它应用的声音,那就设置这个选项,这个选项的周期是从你设置开始到关闭AVAudioSession。
3.如果你的应用程序在其他程序播放音频的时候,也需要用到音频,那还需要使用AVAudioSessionCategoryOptionInterruptSpokenAudioAndMixWithOthers选项。

AVAudioSessionCategoryOptionAllowBluetooth-

1.有效的只有音频会话类是AVAudioSessionCategoryRecord 和 AVAudioSessionCategoryPlayAndRecord
2.允许蓝牙免提设备启用。

AVAudioSessionCategoryOptionDefaultToSpeaker -

1.有效的只有音频会话类只有AVAudioSessionCategoryPlayAndRecord
2.在没有其他的音频路径(如耳机)可以使用的情况下设置这个选项,会议音频将通过设备的内置扬声器播放。当不设置此选项,并没有其他的音频输出可用或选择,音频将通过接收器播放。请注意,只有iPhone设备都配备有一个接收器; iPad和iPod touch设备,此选项没有任何效果。

AVAudioSessionCategoryOptionInterruptSpokenAudioAndMixWithOthers -

1.有效的只有音频会话类是AVAudioSessionCategoryPlayAndRecord, AVAudioSessionCategoryPlayback, 和 AVAudioSessionCategoryMultiRoute
2.如果你的应用程序有偶尔用到音频,并要求用到音频的时候停止其他应用音频,那就设置此选项。

注意:
在用到AVAudioSessionCategoryOptionInterruptSpokenAudioAndMixWithOthers选项时,并中断了其他应用的音频,结束自己的应用音频后,若想恢复其他应用的音频,那就需要在关闭音频会话的时候设置AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation选项:
[audioSession setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:NULL];

贴心提示:以上讲到的两处options是不一样的,setActive:用到的是AVAudioSessionSetActiveOptionssetCategory:用到的是AVAudioSessionCategoryOptions,所以千万别弄混了。

你可能感兴趣的:(iOS - AVAudioSession)