曾经被我们忽略的AVAudioSession

AVAudioSession是用来管理和平衡多个App的(扬声器、麦克疯)的资源的使用。
例如设备在背后进行播放音乐时,这时候用户进入我们的App,需要播放一小段视频时,这个时候,我们应该如何处理?

  • 中断音乐播放?
  • 暂时中断音乐播放,等小视频播放完毕唤醒背后音乐继续播放?
  • 与音乐混音一起播放?
  • 与音乐混音播放暂时压低背后音乐的声音,等小视频播放完毕恢复背后音乐的声音大小?
    这些处理方式都是依靠AVAudioSession来处理的。

1.AVAudioSession 的 Category

我们进入 AVAudioSession 的Category可以发现有7种 Category 可供选择。

AVF_EXPORT NSString *const AVAudioSessionCategoryAmbient;

/*  Use this category for background sounds.  Other music will stop playing. */
AVF_EXPORT NSString *const AVAudioSessionCategorySoloAmbient;

/* Use this category for music tracks.*/
AVF_EXPORT NSString *const AVAudioSessionCategoryPlayback;

/*  Use this category when recording audio. */
AVF_EXPORT NSString *const AVAudioSessionCategoryRecord;

/*  Use this category when recording and playing back audio. */
AVF_EXPORT NSString *const AVAudioSessionCategoryPlayAndRecord;

/*  Use this category when using a hardware codec or signal processor while
 not playing or recording audio. */
AVF_EXPORT NSString *const AVAudioSessionCategoryAudioProcessing NS_DEPRECATED_IOS(3_0, 10_0) __TVOS_PROHIBITED __WATCHOS_PROHIBITED;

/*  Use this category to customize the usage of available audio accessories and built-in audio hardware.
 For example, this category provides an application with the ability to use an available USB output 
 and headphone output simultaneously for separate, distinct streams of audio data. Use of 
 this category by an application requires a more detailed knowledge of, and interaction with, 
 the capabilities of the available audio routes.  May be used for input, output, or both.
 Note that not all output types and output combinations are eligible for multi-route.  Input is limited
 to the last-in input port. Eligible inputs consist of the following:
    AVAudioSessionPortUSBAudio, AVAudioSessionPortHeadsetMic, and AVAudioSessionPortBuiltInMic.  
 Eligible outputs consist of the following: 
    AVAudioSessionPortUSBAudio, AVAudioSessionPortLineOut, AVAudioSessionPortHeadphones, AVAudioSessionPortHDMI, 
    and AVAudioSessionPortBuiltInSpeaker.  
 Note that AVAudioSessionPortBuiltInSpeaker is only allowed to be used when there are no other eligible 
 outputs connected.  */
AVF_EXPORT NSString *const AVAudioSessionCategoryMultiRoute
  • AVAudioSessionCategoryAmbient 仅支持播放,不会打断不支持混音的App,使用这种模式,你的App的声音会与背后的音乐App一起发出声音。 如果是在锁屏或者静音键的情况下你App的音频会终止。
  • AVAudioSessionCategorySoloAmbient 仅支持播放,会打断不支持混音的App,使用这种模式,你的App的声音会打断背后的音乐App。 如果是在锁屏或者静音键的情况下你App的音频会终止。 这种模式是系统的默认模式。
  • AVAudioSessionCategoryPlayback 仅支持播放,默认会打断不支持混音的App。如果是在锁屏或者静音键的情况下你App的音频不会终止。
  • AVAudioSessionCategoryRecord 仅支持录制,会打断不支持混音的App。如果是在锁屏或者静音键的情况下仍可录制。
  • AVAudioSessionCategoryPlayAndRecord 支持播放且支持录制,默认会打断不支持混音的App。如果是在锁屏或者静音键的情况下仍可播放声音或者录制。
  • AVAudioSessionCategoryMultiRoute 支持播放且支持录制,会打断不支持混音的App。如果是在锁屏或者静音键的情况下仍可播放声音或者录制。
  • AVAudioSessionCategoryAudioProcessing 不支持播放且不支持录音,会打断不支持混音的App。iOS10 之后被弃用。

2.AVAudioSession 的 AVAudioSessionCategoryOptions

AVAudioSession 的AVAudioSessionCategoryOptions 同样也是一个枚举。

    /* MixWithOthers is only valid with AVAudioSessionCategoryPlayAndRecord, AVAudioSessionCategoryPlayback, and  AVAudioSessionCategoryMultiRoute */
    AVAudioSessionCategoryOptionMixWithOthers           = 0x1,

    /* DuckOthers is only valid with AVAudioSessionCategoryAmbient, AVAudioSessionCategoryPlayAndRecord, AVAudioSessionCategoryPlayback, and AVAudioSessionCategoryMultiRoute */
    AVAudioSessionCategoryOptionDuckOthers              = 0x2,

    /* AllowBluetooth is only valid with AVAudioSessionCategoryRecord and AVAudioSessionCategoryPlayAndRecord */
    AVAudioSessionCategoryOptionAllowBluetooth  __TVOS_PROHIBITED __WATCHOS_PROHIBITED      = 0x4,

    /* DefaultToSpeaker is only valid with AVAudioSessionCategoryPlayAndRecord */
    AVAudioSessionCategoryOptionDefaultToSpeaker __TVOS_PROHIBITED __WATCHOS_PROHIBITED     = 0x8,

    /* InterruptSpokenAudioAndMixWithOthers is only valid with AVAudioSessionCategoryPlayAndRecord, AVAudioSessionCategoryPlayback, and AVAudioSessionCategoryMultiRoute */
    AVAudioSessionCategoryOptionInterruptSpokenAudioAndMixWithOthers NS_AVAILABLE_IOS(9_0) = 0x11,

    /* AllowBluetoothA2DP is only valid with AVAudioSessionCategoryPlayAndRecord */
    AVAudioSessionCategoryOptionAllowBluetoothA2DP API_AVAILABLE(ios(10.0), watchos(3.0), tvos(10.0)) = 0x20,

    /* AllowAirPlay is only valid with AVAudioSessionCategoryPlayAndRecord */
    AVAudioSessionCategoryOptionAllowAirPlay API_AVAILABLE(ios(10.0), tvos(10.0)) __WATCHOS_PROHIBITED = 0x40,
  • AVAudioSessionCategoryOptionMixWithOthers 当你的App包含声音播放时,设置这个选项在激活会话时不会打断其他App的音频播放。
    适用于以下category:
    • AVAudioSessionCategoryPlayback
    • AVAudioSessionCategoryPlayAndRecord
    • AVAudioSessionCategoryMultiRoute
  • AVAudioSessionCategoryOptionDuckOthers 当你的App包含声音播放时,设置这个选项在激活会话时会降低其他App的声音播放。
    适用于以下category:
    • AVAudioSessionCategoryAmbient
    • AVAudioSessionCategoryPlayAndRecord
    • AVAudioSessionCategoryPlayback
    • AVAudioSessionCategoryMultiRoute
  • AVAudioSessionCategoryOptionAllowBluetooth 允许可免提蓝牙设备可使用输入通道
    适用于以下category:
    • AVAudioSessionCategoryRecord
    • AVAudioSessionCategoryPlayAndRecord
  • AVAudioSessionCategoryOptionDefaultToSpeaker 在没有其他通道的时候默认选择内置扬声器
    适用于以下category:
    • AVAudioSessionCategoryPlayAndRecord
  • AVAudioSessionCategoryOptionInterruptSpokenAudioAndMixWithOthers 你的App偶尔的使用音频播放。
    适用于以下category:
    • AVAudioSessionCategoryPlayback
    • AVAudioSessionCategoryPlayAndRecord
    • AVAudioSessionCategoryMultiRoute
  • AVAudioSessionCategoryOptionAllowBluetoothA2DP 立体声蓝牙。
    适用于以下category:
    • AVAudioSessionCategoryPlayAndRecord
  • AVAudioSessionCategoryOptionAllowAirPlay 远程AirPlay设备。
    适用于以下category:
    • AVAudioSessionCategoryPlayAndRecord

3. 使用AVAudioSession 的 Category 和 AVAudioSessionCategoryOptions 调整各种音频模式

1.播放音视频的时候直接中断背后的音乐播放。

这种情况下,我们直接将category 设置为 AVAudioSessionCategorySoloAmbient即可,当然你不做任何处理系统也会进行默认模式处理。很多App也是这种模式。

    AVAudioSession *session = [AVAudioSession sharedInstance];
    NSError * error = nil;
    [session setCategory:AVAudioSessionCategorySoloAmbient error:&error];
    if (error) {
        NSLog(@"%@",error);
    }

2.播放音视频的时候暂时中断背后的音乐,播放完毕后再继续背后的音乐。

这种情况下,category设置与1的相同,然后在播放前 增加下面的代码告诉背后的音乐App你将要占用音频焦点。

[[AVAudioSession sharedInstance] setActive:YES withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];

在暂停播放或者App退到后台后暂停播放前,告诉背后的音乐App你取消音频焦点的占用。

[[AVAudioSession sharedInstance] setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];

3.播放音视频的时候与背后音乐App混音一起播放

这种情况下,我们直接将category 设置为 AVAudioSessionCategoryAmbient即可。

    AVAudioSession *session = [AVAudioSession sharedInstance];
    NSError * error = nil;
    [session setCategory:AVAudioSessionCategoryAmbient error:&error];
    if (error) {
        NSLog(@"%@",error);
    }

4.播放音视频的时候暂时压低背后音乐的声音,等音视频播放完毕恢复背后音乐的声音大小

这种情况下,需要设置category为AVAudioSessionCategoryAmbient,AVAudioSessionCategoryOptions设置为AVAudioSessionCategoryOptionDuckOthers

 AVAudioSession *session = [AVAudioSession sharedInstance];
    NSError * error = nil;
    [session setCategory:AVAudioSessionCategoryAmbient withOptions:AVAudioSessionCategoryOptionDuckOthers error:&error];
    if (error) {
        NSLog(@"%@",error);
    }

然后在播放前 增加下面的代码告诉背后的音乐App你将要占用音频焦点。

[[AVAudioSession sharedInstance] setActive:YES withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];

在暂停播放或者App退到后台后暂停播放前,告诉背后的音乐App你取消音频焦点的占用。

[[AVAudioSession sharedInstance] setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];

以上是一些AVAudioSession 的 Category 和 AVAudioSessionCategoryOptions 配合使用的几种模式的举例,具体要根据自己的实际需要进行调整。Demo下载地址

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
AVAudioSession是iOS中用于管理应用程序的音频会话的类。它允许应用程序控制和配置音频功能,如音频输入、输出、音频路由、音频处理等。AVAudioSession的目的是提供一个统一的接口,以便应用程序可以与设备的音频系统进行交互,无论是播放音乐、录制语音还是进行VoIP通话。 AVAudioSession有几个重要的概念和属性: 1. Category(分类):用于描述应用程序对音频的使用情况,如播放音乐、录制音频、播放通话等。不同的分类具有不同的优先级和行为。应用程序可以根据需要选择适当的分类,并在需要时进行切换。 2. Mode(模式):用于描述音频会话的详细行为。不同的模式针对不同的应用场景进行了优化,如游戏、音乐播放、通话等。应用程序可以根据需要设置适当的模式。 3. Route(路由):描述音频信号的路径,如扬声器、听筒、耳机等。应用程序可以查询当前的音频路由,并根据需要进行调整。 4. Options(选项):用于配置音频会话的其他参数,如混音、静音、中断处理等。 AVAudioSession还提供了一些其他的功能和方法,如设置音频输入、输出的硬件设备、监测耳机插拔事件、处理音频会话的中断和恢复等。 总之,AVAudioSession是一个强大的音频管理类,允许应用程序轻松控制和配置音频功能,并为各种应用场景提供了灵活的接口。通过使用AVAudioSession,开发者可以实现各种复杂的音频操作,提供优质的音频体验给用户。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值