AVAudioSession学习记录

本文介绍了iOS中AVAudioSession相关的枚举类型及其用途,包括中断选项、激活选项、端口覆盖、路由变更原因等,帮助开发者更好地理解并利用音频会话API。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、枚举(enum):

   1. AVAudioSessionInterruptionOptions:只有一个枚举值AVAudioSessionInterruptionOptionShouldResume,用于中断音频之后,重新激活之前播放的音频。在音频中断时,系统会发送AVAudioSessionInterruptionNotification通知,userInfo中AVAudioSessionInterruptionTypeKeyAVAudioSessionInterruptionTypeKeyEnded时,表明中断结束,再获取userInfo中AVAudioSessionInterruptionOptions值为AVAudioSessionInterruptionOptionShouldResume,表明可以重新激活音频,并进行相应的处理。

2.AVAudioSessionSetActiveOptions只有一个枚举值AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation,用于setActive:withOptions:error:方法的Options参数中,当前App音频结束不再处于活跃状态时,通知系统将之前中断的音频(如music app),重新激活。

3.AVAudioSessionPortOverride枚举值AVAudioSessionPortOverrideNoneAVAudioSessionPortOverrideSpeaker。用于设置当前音频输出端口,None,不覆盖输出端口,使用当前默认的音频类别的端口;Speaker,覆盖当前端口,将音频输出端口转换到扬声器,仅适用于AVAudioSessionCategoryPlayAndRecord类别。

4.AVAudioSessionRouteChangeReason

枚举值:

AVAudioSessionRouteChangeReasonUnknown

AVAudioSessionRouteChangeReasonNewDeviceAvailable(如耳机插入)

AVAudioSessionRouteChangeReasonOldDeviceUnavailable(如耳机拔出)

AVAudioSessionRouteChangeReasonCategoryChange(如类别变化,AVAudioSessionCategoryPlayback改成AVAudioSessionCategoryPlayAndRecord

AVAudioSessionRouteChangeReasonOverride

AVAudioSessionRouteChangeReasonWakeFromSleep

AVAudioSessionRouteChangeReasonNoSuitableRouteForCategory

AVAudioSessionRouteChangeReasonRouteConfigurationChange

多用于监听(AVAudioSessionRouteChangeNotification)耳机等设备变化之后,是否暂停播放音频。

5.AVAudioSessionCategoryOptions

枚举值:

AVAudioSessionCategoryOptionMixWithOthers(当你的audio session活跃时,允许混合其他应用音频。典型场景:a. 在 AVAudioSessionCategoryPlayAndRecord or AVAudioSessionCategoryMultiRoute 类别下设置,表示audio的输入输出都enabled下允许其他应用在后台播放;b. 在 AVAudioSessionCategoryPlayback 类别下设置,表示允许其他应用后台播放,但是无论如何切换 静音/铃声 都会一直播放;c. 在其他类别下设置无效

AVAudioSessionCategoryOptionDuckOthers(当你的audio session活跃时,会混合其他应用的音频,但是会降低其他音频的音量,直到你的audio session不活跃。仅在AVAudioSessionCategoryPlayAndRecord、AVAudioSessionCategoryPlayback、AVAudioSessionCategoryMultiRoute、AVAudioSessionCategoryAmbient类别下有效

AVAudioSessionCategoryOptionAllowBluetooth(可接入蓝牙设备。a. 在AVAudioSessionCategoryPlayAndRecord类别下,当用合适的输出设备播放时,可用配对的蓝牙设备进行输入;b.AVAudioSessionCategoryRecord类别下,可用配对的蓝牙设备进行输入;c.在其他类别下设置无效

AVAudioSessionCategoryOptionDefaultToSpeaker(只在AVAudioSessionCategoryPlayAndRecord类别下有效,设置扬声器播放

AVAudioSessionCategoryOptionInterruptSpokenAudioAndMixWithOthers(当你的app是导航类或者运动类应用,偶然说句提示语,仅在AVAudioSessionCategoryPlayAndRecord、AVAudioSessionCategoryPlayback、AVAudioSessionCategoryMultiRoute类别下有效

AVAudioSessionCategoryOptionAllowBluetoothA2DP(可接入蓝牙设备。a. 在AVAudioSessionCategoryPlayAndRecord类别下,当用合适的输入设备录制时,可用配对的蓝牙设备进行输出;b.在AVAudioSessionCategoryMultiRoute orAVAudioSessionCategoryRecord类别下,不能被设置;c. 在其他类别下默认设置为true且不能更改

AVAudioSessionCategoryOptionAllowAirPlay(只在AVAudioSessionCategoryPlayAndRecord类别下有效

用于setCategory:withOptions:error:方法中,设置options选项。


6.AVAudioSessionInterruptionType:枚举值AVAudioSessionInterruptionTypeBeganAVAudioSessionInterruptionTypeEnded,表示中断类型,用于判断中断开始或者结束。用AVAudioSessionInterruptionNotification进行通知。


7.AVAudioSessionSilenceSecondaryAudioHintType:枚举值AVAudioSessionSilenceSecondaryAudioHintTypeBeginAVAudioSessionSilenceSecondaryAudioHintTypeEnd,表示其他应用开始占据session,用AVAudioSessionSilenceSecondaryAudioHintNotification进行通知。


8.AVAudioSessionRecordPermission

枚举值:

AVAudioSessionRecordPermissionUndetermined(用户未确定是否授权)

AVAudioSessionRecordPermissionDenied(用户明确拒绝授权)

AVAudioSessionRecordPermissionGranted(用户明确授权)

用来判断用户是否授权录制权限。


9.AVAudioSessionIOType:枚举值AVAudioSessionIOTypeNotSpecifiedAVAudioSessionIOTypeAggregated,用于setAggregatedIOPreference:error:方法。


10.AVAudioSessionRouteSharingPolicy

枚举值:

AVAudioSessionRouteSharingPolicyDefault

AVAudioSessionRouteSharingPolicyLongForm

AVAudioSessionRouteSharingPolicyIndependent





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值