作者:消瘦了一生 | 来源:互联网 | 2023-08-16 16:43
AVAudioSession在应用程序与操作系统之间扮演中间人的角色。提供一种简单的方法使应用程序知道如何与iOS音频环境进行交互。插入耳机,音频正常播放,但是Apple规定,耳机
AVAudioSession 在应用程序与操作系统之间扮演中间人的角色。提供一种简单的方法使应用程序知道如何与iOS音频环境进行交互。
AVAudioSession可以通过分类控制一些音频行为:
分类作用是否支持混音音频输入音频输出Ambient游戏、效率YNYSolo Ambient(默认)游戏、效率NNYPlayback音频、视频播放器可选NYRecord录音机、音频捕捉NYNPlay and RecordVoIP、语音聊天率可选YYAudio Precessing离线会话和处理NNNMulti-Route使用外部硬件的高级A/V应用程序NYY // 初始化session AVAudioSession *session = [AVAudioSession sharedInstance]; NSError *error; if ([session setCategory:AVAudioSessionCategoryPlayback error:&error]) { NSLog(@"AudioSession setcategory error: %@", error); } if ([session setActive:YES error:&error]) { NSLog(@"AudioSession setActive error: %@", error); } AVAudioPlayer AVAudioPlayer 可以播放除网络音频、原始音频或者非常低的延时之外的所有情况
初始化:
- (AVAudioPlayer *)play{ if(!_play){ NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"guitar" withExtension:@"caf"]; _play = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:NULL]; _play.delegate = self; [_play prepareToPlay]; } return _play;}
方法:
play (播放)pause (暂停)stop (停止,和pause不同指出会清除prepareToPlay的配置)
属性:
volume: 音量pan:声道numberOfLoops: 循环次数 (-1无限循环)rate: 播放速率(需要开启enableRate) 后台播放 session category 设置为AVAudioSessionCategoryPlaybackinfo.plist 添加:Required background modes - App plays audio or streams audio/video using AirPlay 中断处理 音频播放过程中,会被电话、faceTime导致中断,系统中断时会发送`AVAudioSessionInterruptionNotification`通知。
注册通知:
NSNotificationCenter *nsnc = [NSNotificationCenter defaultCenter];[nsnc addObserver:self selector:@selector(handleInterrupt:) name:AVAudioSessionInterruptionNotification object:[AVAudioSession sharedInstance]];
方法实现:
- (void)handleInterrupt:(NSNotification *)notification{ NSDictionary *info = notification.userInfo; AVAudioSessionInterruptionType type = [info[AVAudioSessionInterruptionTypeKey] unsignedIntegerValue]; if (type == AVAudioSessionInterruptionTypeBegan) { // 中断开始(更新UI状态 暂停播放) }else{ // 中断结束 AVAudioSessionInterruptionOptions option = [info[AVAudioSessionInterruptionOptionKey] unsignedIntegerValue]; if (option == AVAudioSessionInterruptionOptionShouldResume) { // 是否再次播放 } }} Audio channel 处理 音频播放过程中,插入耳机等会导致播放渠道的更改。插入耳机,音频正常播放,但是Apple规定,耳机内播放的内容可能是隐私信息。因此,拔掉耳机,音频应该是暂停状态。整个过程系统会发送`AVAudioSessionRouteChangeNotification`通知,供应用处理。
注册通知:
NSNotificationCenter *nsnc = [NSNotificationCenter defaultCenter];[nsnc addObserver:self selector:@selector(handleRouteChange:) name:AVAudioSessionRouteChangeNotification object:[AVAudioSession sharedInstance]];
通知实现:
- (void)handleRouteChange:(NSNotification *)notification { NSDictionary *info = notification.userInfo; AVAudioSessionRouteChangeReason reason = [info[AVAudioSessionRouteChangeReasonKey] unsignedIntValue]; if (reason == AVAudioSessionRouteChangeReasonOldDeviceUnavailable) { // 耳机断开事件 // 获取前一个channel信息 AVAudioSessionRouteDescription *previousRoute = info[AVAudioSessionRouteChangePreviousRouteKey]; AVAudioSessionPortDescription *previousOutput = previousRoute.outputs[0]; NSString *portType = previousOutput.portType; if ([portType isEqualToString:AVAudioSessionPortHeadphones]) { // 前一个channel输出是耳机类型,暂停播放 } }}