AVAudioSession简单介绍

当我们在iOS上播放音频的时候.如果来了个电话,那么音频就会自动暂停.当电话结束之后,音频又会自动播放.在用户看来也就是一个神奇的魔法而已.然而在我们开发人员角度来说,我们需要知道iOS系统上有一种managed audio environment(可管理的音频环境).它会智能的管理音频环境.对于开发者来说就是AVAudioSession.

AvaudioSession是一个单例,设置方法有如下四种

- (BOOL)setCategory:(AVAudioSessionCategory)category error:(NSError **)outError API_AVAILABLE(ios(3.0), watchos(2.0), tvos(9.0)) API_UNAVAILABLE(macos);

- (BOOL)setCategory:(AVAudioSessionCategory)category withOptions:(AVAudioSessionCategoryOptions)options error:(NSError **)outError API_AVAILABLE(ios(6.0), watchos(2.0), tvos(9.0)) API_UNAVAILABLE(macos);

- (BOOL)setCategory:(AVAudioSessionCategory)category mode:(AVAudioSessionMode)mode options:(AVAudioSessionCategoryOptions)options error:(NSError **)outError API_AVAILABLE(ios(10.0), watchos(3.0), tvos(10.0)) API_UNAVAILABLE(macos);

- (BOOL)setCategory:(AVAudioSessionCategory)category mode:(AVAudioSessionMode)mode routeSharingPolicy:(AVAudioSessionRouteSharingPolicy)policy options:(AVAudioSessionCategoryOptions)options error:(NSError **)outError API_AVAILABLE(ios(11.0), tvos(11.0), watchos(5.0)) API_UNAVAILABLE(macos);

AVAudioSessionCategory

/// 用这个分类来发出一些类似雨声、骑车引擎噪音等.可以与其他音乐混合使用
// extern AVAudioSessionCategory const AVAudioSessionCategoryAmbient API_AVAILABLE(ios(3.0), watchos(2.0), tvos(9.0)) API_UNAVAILABLE(macos);

/// 此分类用于背景音,会暂停其他音乐的播放
// extern AVAudioSessionCategory const AVAudioSessionCategorySoloAmbient API_AVAILABLE(ios(3.0), watchos(2.0), tvos(9.0)) API_UNAVAILABLE(macos);

/// 此分类用来播放音乐曲目
// extern AVAudioSessionCategory const AVAudioSessionCategoryPlayback API_AVAILABLE(ios(3.0), watchos(2.0), tvos(9.0)) API_UNAVAILABLE(macos);

/// 用此分类来录制音频
// extern AVAudioSessionCategory const AVAudioSessionCategoryRecord API_AVAILABLE(ios(3.0), watchos(2.0), tvos(9.0)) API_UNAVAILABLE(macos);

/// 录制和播放音频的时候使用此分类
// extern AVAudioSessionCategory const AVAudioSessionCategoryPlayAndRecord API_AVAILABLE(ios(3.0), watchos(2.0), tvos(9.0)) API_UNAVAILABLE(macos);

/// 使用硬件解码器或者使用信号处理器的时候使用此类别.(注:不能播放或者录制音频)
//    extern AVAudioSessionCategory const AVAudioSessionCategoryAudioProcessing API_DEPRECATED("No longer supported", ios(3.0, 10.0)) API_UNAVAILABLE(watchos, tvos) API_UNAVAILABLE(macos);

/ *使用此类别可自定义可用音频配件和内置音频硬件的使用情况。
例如,此类别为应用程序提供了使用可用USB输出的功能
和耳机同时输出单独的,不同的音频数据流。用于
应用程序要求此类别需要更详细的知识和与之交互,
可用音频路由的功能。可用于输入,输出或两者。
请注意,并非所有输出类型和输出组合都符合多路径的条件。输入有限
到最后输入端口。符合条件的输入包括以下内容:
AVAudioSessionPortUSBAudio,AVAudioSessionPortHeadsetMic和AVAudioSessionPortBuiltInMic。
符合条件的输出包括以下内容:
AVAudioSessionPortUSBAudio,AVAudioSessionPortLineOut,AVAudioSessionPortHeadphones,AVAudioSessionPortHDMI,
和AVAudioSessionPortBuiltInSpeaker。
请注意,只有在没有其他符合条件的情况下才允许使用AVAudioSessionPortBuiltInSpeaker
输出连接。 * /
// extern AVAudioSessionCategory const AVAudioSessionCategoryMultiRoute API_AVAILABLE(ios(6.0), watchos(2.0), tvos(9.0)) API_UNAVAILABLE(macos);
分类 作用 音频输入 音频输出 是否允许混音 是否会被静音键或者是锁屏键静音
Ambient 游戏、效率应用程序 ✔️ ✔️ ✔️
Solo Ambient (默认) 游戏、效率应用程序 ✔️ ✔️
Playback 音频和视频播放器 ✔️ 可选,默认❌
Record 录音机、音频捕捉 ✔️ ❌ 锁屏下任然可以继续录制
PlayAndRecord VoIP、语音聊天(类似QQ语音) ✔️ ✔️ 可选,默认❌
AudioProcessing (iOS 3.0-10.0,已弃用) 离线会话和处理
MultiRoute 使用外部硬件的高级A/V应用程序 ✔️ ✔️

注:锁屏键或者是应用不在Active状态还能播放音频的category需要设置UIBackgroundModes

AVAudioSessionCategoryOption (用于微调Category行为)

CategoryOption 兼容的Category 功能
MixWithOthers PlayAndRecord、Playback、MultiRoute 支持和其他App音频混音
DuckOthers Ambient、PlayAndRecord、Playback、MultiRoute 系统智能调低其他App音频音量
AllowBluetooth Record、PlayAndRecord 支持蓝牙音频输入
DefaultToSpeaker PlayAndRecord 设置默认输出音频到扬声器
InterruptSpokenAudioAndMixWithOthers (iOS 9 -) PlayAndRecord、Playback、MultiRoute DuckOthers类似
AllowBluetoothA2DP (iOS 10 -) PlayAndRecord 允许蓝牙A2DP设备
AllowAirPlay (iOS 10 -) PlayAndRecord 允许AirPlay

AVAudioSessionMode (可以通过mode来定制Category的行为)

AudioSessionMode 兼容category 应用场景
Default All Category 默认音频会话模式
VoiceChat PlayAndRecord VoIP(双向语音通信)
GameChat PlayAndRecord 游戏录制(Game Kit)
VideoRecording Record、PlayAndRecord 录制视频
Measurement Playback、Record、PlayAndRecord 音频输入或输出的测量
MoviePlayback Playback 视频播放
VideoChat PlayAndRecord 视频通话
SpokenAudio Playback、SoloAmbient、PlayAndRecord、MultiRoute 有声读物
VoicePrompt

AVAudioSessionRouteSharingPolicy (个人理解是设置成哪个音频通道优先)

AVAudioSessionRouteSharingPolicy 作用
Default 路由音频输出常规规则
LongFormAudio 将输出路由到共享的长格式音频输出
LongForm (已废弃,用上面的LongFormAudio)
Independent 应用程序不应尝试直接设置此值。在iOS上,此值将设置为在路由选择器UI用于将视频定向到无线路由的情况下的系统。
LongFormVideo 将输出路由到共享的长格式视频输出。(需要在info.plist里头加@{@“AVInitialRouteSharingPolicy” ?“字符串”})

注:AVInitialRouteSharingPolicy(AirPlay optimization policy).说明是String.感觉应该是使用AirPlay

active/deactive当前配置

- (BOOL)setActive:(BOOL)active error:(NSError **)outError API_AVAILABLE(ios(3.0), watchos(2.0), tvos(9.0)) API_UNAVAILABLE(macos);
- (BOOL)setActive:(BOOL)active withOptions:(AVAudioSessionSetActiveOptions)options error:(NSError **)outError API_AVAILABLE(ios(6.0), watchos(2.0), tvos(9.0)) API_UNAVAILABLE(macos);

用法

AVAudioSession *session = [AVAudioSession sharedInstance];
NSError *error = nil;
if (![session setCategory:AVAudioSessionCategoryAmbient error:&error]) {
	// 处理错误
}
NSError *errorActive = nil;
if (![session setActive:YES error:&errorActive]) {
	// 处理错误
}
发布了268 篇原创文章 · 获赞 59 · 访问量 19万+

猜你喜欢

转载自blog.csdn.net/qq_18683985/article/details/98815689