AVAudioSession通知


# 通知

/// 被打断通知
extern NSNotificationName const AVAudioSessionInterruptionNotification API_AVAILABLE(ios(6.0), watchos(2.0), tvos(9.0)) API_UNAVAILABLE(macos);

/// 路线改变通知(如,扬声器切换为耳机.也就是耳机插入设备)
extern NSNotificationName const AVAudioSessionRouteChangeNotification API_AVAILABLE(ios(6.0), watchos(2.0), tvos(9.0)) API_UNAVAILABLE(macos);

/// 当媒体服务丢失的时候的通知.
extern NSNotificationName const AVAudioSessionMediaServicesWereLostNotification API_AVAILABLE(ios(7.0), watchos(2.0), tvos(9.0)) API_UNAVAILABLE(macos);

/// 媒体服务重启时的通知
extern NSNotificationName const AVAudioSessionMediaServicesWereResetNotification API_AVAILABLE(ios(6.0), watchos(2.0), tvos(9.0)) API_UNAVAILABLE(macos);

/// 其他App占用/解除占用AVAudioSession通知
extern NSNotificationName const AVAudioSessionSilenceSecondaryAudioHintNotification API_AVAILABLE(ios(8.0), watchos(2.0), tvos(9.0)) API_UNAVAILABLE(macos);

# 通知中userInfo对应的key

/// `AVAudioSessionInterruptionNotification`的键,表示被中断的类型(AVAudioSessionInterruptionType)
extern NSString *const AVAudioSessionInterruptionTypeKey API_AVAILABLE(ios(6.0), watchos(2.0), tvos(9.0)) API_UNAVAILABLE(macos);

/// 仅用于中断事件中,类型为`AVAudioSessionInterruptionOptions `
extern NSString *const AVAudioSessionInterruptionOptionKey API_AVAILABLE(ios(6.0), watchos(2.0), tvos(9.0)) API_UNAVAILABLE(macos);

/// 仅仅在打断事件中,值为BOOL类型.ture表示是系统暂停的,false表示是其他的音频会话终端的.
extern NSString *const AVAudioSessionInterruptionWasSuspendedKey API_AVAILABLE(ios(10.3), watchos(2.3), tvos(10.3)) API_UNAVAILABLE(macos);

/// 用于`AVAudioSessionRouteChangeNotification`.值的类型是NSNumber.
extern NSString *const AVAudioSessionRouteChangeReasonKey API_AVAILABLE(ios(6.0), watchos(2.0), tvos(9.0)) API_UNAVAILABLE(macos);

/// 值是`AVAudioSessionRouteDescription`
extern NSString *const AVAudioSessionRouteChangePreviousRouteKey API_AVAILABLE(ios(6.0), watchos(2.0), tvos(9.0)) API_UNAVAILABLE(macos);

/// 用于`AVAudioSessionSilenceSecondaryAudioHintNotification`.值的类型是NSNumber.
extern NSString *const AVAudioSessionSilenceSecondaryAudioHintTypeKey API_AVAILABLE(ios(8.0), watchos(2.0), tvos(9.0)) API_UNAVAILABLE(macos);

AVAudioSessionInterruptionNotification (音频被中断通知)

/// MARK: 用到的枚举
typedef NS_ENUM(NSUInteger, AVAudioSessionInterruptionType)
{
	AVAudioSessionInterruptionTypeBegan = 1,  /* the system has interrupted your audio session */
	AVAudioSessionInterruptionTypeEnded = 0,  /* the interruption has ended */
};

/// MARK: 使用方式

/// 添加通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioSessionInterruptionNotification:) name:AVAudioSessionInterruptionNotification object:nil];

- (void)audioSessionInterruptionNotification:(NSNotification *)noti {
    NSInteger InterruptionType = [[noti.userInfo objectForKey:AVAudioSessionInterruptionTypeKey] integerValue];
    switch (InterruptionType) {
        case AVAudioSessionInterruptionTypeBegan: {
            NSLog(@"开始打断");
        }
            break;
        case AVAudioSessionInterruptionTypeEnded: {
            NSLog(@"结束打断");
        }
            break;
        default:
            break;
    }

}

当我们的App正在播放音频,然后我们开启了其他App播放音乐或者是突然来电话了,那么我们就会收到一个AVAudioSessionInterruptionNotification通知,然后当我们应用重新进入前台.也就是active状态的时候.我们会收到一个AVAudioSessionInterruptionTypeEnded.告诉我们打断已经结束.可以重新让我们的播放器继续播放了.(一定要重新进入前台).

noti.userInfo @{@"AVAudioSessionRouteChangePreviousRouteKey" :<id AVAudioSessionRouteDescription> ,@"AVAudioSessionRouteChangeReasonKey" :@()}

/// 其中AVAudioSessionRouteChangePreviousRouteKey

AVAudioSessionRouteChangeNotification (播放线路改变通知)

当抽插耳机等造成播放线路改变时会发送的通知

userInfo信息:

- (void)audioSessionRouteChangeNotification:(NSNotification *)noti {
    AVAudioSessionRouteDescription *routeDescription = [noti.userInfo valueForKey:AVAudioSessionRouteChangePreviousRouteKey];
    NSInteger reason = [[noti.userInfo valueForKey:AVAudioSessionRouteChangeReasonKey] integerValue];

}
typedef NS_ENUM(NSUInteger, AVAudioSessionRouteChangeReason)
{
	/// 未知原因
	AVAudioSessionRouteChangeReasonUnknown = 0,
	/// 有新设备可用
	AVAudioSessionRouteChangeReasonNewDeviceAvailable = 1,
	/// 老设备不可用
	AVAudioSessionRouteChangeReasonOldDeviceUnavailable = 2,
	/// 类别改变
	AVAudioSessionRouteChangeReasonCategoryChange = 3,
	/// App重置了输出设置
	AVAudioSessionRouteChangeReasonOverride = 4,
	/// 从睡眠状态唤醒
	AVAudioSessionRouteChangeReasonWakeFromSleep = 6,
	/// 当前category下没合适设备
	AVAudioSessionRouteChangeReasonNoSuitableRouteForCategory = 7,
	/// Rotuer的配置改变了
	AVAudioSessionRouteChangeReasonRouteConfigurationChange = 8 // added in iOS 7
};

AVAudioSessionMediaServicesWereLostNotification (媒体服务丢失通知)

详情

使用系统内置Developer功能的时候,会过一会儿才收到通知.此通知无userInfo.我们需要做的就是收到通知后看情况是否重启媒体服务.

AVAudioSessionMediaServicesWereResetNotification (媒体服务重启通知)

详情见上面的AVAudioSessionMediaServicesWereLostNotification详情.

AVAudioSessionSilenceSecondaryAudioHintNotification

其他设备占用了AVAudioSession通过userInfo中的AVAudioSessionSilenceSecondaryAudioHintTypeKey获取到的值有以下两种可能(类似上面的音频播放被中断)

AVAudioSessionSilenceSecondaryAudioHintTypeBegin 其他App开始占据Session
AVAudioSessionSilenceSecondaryAudioHintTypeEnd 其他App停止占用Session
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值