曾经被我们忽略的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下载地址

猜你喜欢

转载自blog.csdn.net/mandagod/article/details/84959772