AV Foundation-音频播放

AVAudioSession

在应用程序与操作系统之间扮演中间人的角色。提供一种简单的方法使应用程序知道如何与iOS音频环境进行交互。
复制代码

AVAudioSession可以通过分类控制一些音频行为:

分类作用是否支持混音音频输入音频输出
Ambient游戏、效率YNY
Solo Ambient(默认)游戏、效率NNY
Playback音频、视频播放器可选NY
Record录音机、音频捕捉NYN
Play and RecordVoIP、语音聊天率可选YY
Audio Precessing离线会话和处理NNN
Multi-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

后台播放

  1. session category 设置为AVAudioSessionCategoryPlayback
  2. info.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输出是耳机类型,暂停播放
        }
    }
}
复制代码
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值