AVFoundation之音频的播放和录制

一.音频播放

1.播放音频

所有的iOS应用程序都具有音频会话,无论是否使用.默认的音频会话都会有一些预配置.

  • 当用户切换成静音模式,应用程序的播放的所有音频都会消失
  • 当应用程序播放音频的时候,所有的后台播放的音频都会处于静音状态

音频会话分类

AVFoundation 定义了7种分类来描述应用程序所使用的音频行为.

分类 作用 是否允许混音 音频输入 音频输出
Ambient 游戏.效率应用程序
Solo Ambient(默认) 游戏.效率应用程序
PlayBack 音频和视频播放器 可选
Record 录音机.音频捕捉
Player and Record VoIP.语音聊天 可选
Audio Processing 离线会话和处理
Multi-Route 使用外部硬件的高级程序

根据应用程序的选择合适的分类.

配置会话的最佳位置就是在application:didFinishLaunchingWithOptions:方法中...

    AVAudioSession *session = [AVAudioSession sharedInstance];
    
    NSError *error;
    
    //使用playBack分类,应用程序允许将输出音频和背景声音进行混合
    if (![session setCategory:AVAudioSessionCategoryPlayback error:&error]) {
        NSLog(@"category Error : %@",[error localizedDescription]);
    }
    
    if (![session setActive:YES error:&error]) {
        NSLog(@"Activation Error : %@",[error localizedDescription]);
    }
    

AVAudioPlayer构建于Core Audio 最顶层,提供一下播放,暂停,循环甚至音频计量等功能.但是无法直接播放网络音频.
简单的实例化:

 //caf 是苹果通用的音乐容器
    NSURL *fileURL = [[NSBundle mainBundle] URLForResource:fileName withExtension:@"caf"];
    NSError *error;
    
    AVAudioPlayer *avAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&error];
    
    if (avAudio) {
        //-1(负数)表示循环播放 ,0表示播放一次,1表示播放两次,依次类推
        avAudio.numberOfLoops = -1;
        //YES-对播放率进行控制  NO-不对播放率进行控制
        avAudio.enableRate = YES;
        //启动播放前的初始化
        [avAudio prepareToPlay];
    }
    else
    {
        NSLog(@"ERROR creating player: %@",[error localizedDescription]);
    }
    

prepareToPlay会取得需要的音频硬件并预加载到 Audio Queue 的缓冲区.可降低调用play和听到声音之间的延迟.

值得注意的是stoppause都可以停止当前播放行为.两者的主要区别是stop方法会撤销调用 prepareToPlay 所做的设置,而调用 pause 方法则不会

等配置设置完成后,就可以调用play方法,播放音频.

-(void)play{

    if (!self.isPlaying) {
       [player play];
        self.playing = YES;
    }
    
}
2.处理中断事件

在音频处于播放状态时,当手机接到电话等原因导致音频中断,但是当中断事件停止,音频播放没有正常恢复时,就需要我们手动处理这些问题.

当发生中断的时候,音频会话AVAudioSession会发送通知AVAudioSessionInterruptionNotification.需要在控制器初始化时注册该通知

        
        //处理中断事件
        NSNotificationCenter *nsnc = [NSNotificationCenter defaultCenter];
        
        [nsnc addObserver:self
                 selector:@selector(handleInterruption:)
                     name:AVAudioSessionInterruptionNotification
                   object:[AVAudioSession sharedInstance]];
        

然后在通知方法里面检测AVAudioSessionInterruptionTypeKey确定中断类型(AVAudioSessionInterruptionType),从而判断中断开始或者结束;

#pragma mark --处理中断事件--
-(void)handleInterruption:(NSNotification *)notification
{
    
    NSDictionary *info = notification.userInfo;
    
    AVAudioSessionInterruptionType type = [info[AVAudioSessionInterruptionTypeKey] integerValue];
    
    switch (type) {
            //中断开始
        case AVAudioSessionInterruptionTypeBegan:
        {
            [self stop];
            //调用代理 停止
            !self.delegate?:[self.delegate playbackStopped];
        }
            break;
            //中断结束
        case AVAudioSessionInterruptionTypeEnded:
        {
            AVAudioSessionInterruptionOptions option = [info[AVAudioSessionInterruptionOptionKey] unsignedIntegerValue];
            //表明音频会话是否已经重新激活以及是否可以再次播放
            if (option == AVAudioSessionInterruptionOptionShouldResume) {
                [self play];
                
                !self.delegate?:[self.delegate playbackBegin];
            }
            
            
            
        }
            break;
            
        default:
            break;
    }


}

3.对线路改变的相应

在iOS设备上插入或者移除耳机的时候,会发生线路的改变.同时AVAudioSession会发出通知AVAudioSessionRouteChangeNotification.我们需要在接收到通知的时,做出相应的处理,以保证用户隐私,提高用户体验.

        NSNotificationCenter *nsnc = [NSNotificationCenter defaultCenter];
 
        //处理输入设备事件
        [nsnc addObserver:self
                 selector:@selector(handleRouteChange:)
                     name:AVAudioSessionRouteChangeNotification
                   object:[AVAudioSession sharedInstance]];

接收到通知,查看线路变更的原因(AVAudioSessionRouteChangeReason).从而做出相应的处理

#pragma mark --处理耳机设备更换事件--
-(void)handleRouteChange:(NSNotification *)notification
{
    
    NSDictionary *info = notification.userInfo;
    
    AVAudioSessionRouteChangeReason reason = [info[AVAudioSessionRouteChangeReasonKey] unsignedIntegerValue];
    //旧设备不可用
    if (reason == AVAudioSessionRouteChangeReasonOldDeviceUnavailable) {
        //远程设备的描述
        AVAudioSessionRouteDescription *previousRoute = info[AVAudioSessionRouteChangePreviousRouteKey];
        //设备
        AVAudioSessionPortDescription *port = previousRoute.outputs[0];
        
        if ([port.portType isEqualToString:AVAudioSessionPortHeadphones]) {
            
            [self stop];
            
            !self.delegate?:[self.delegate playbackStopped];
            
        }

    }

}

源码来至 : github地址

你可能感兴趣的:(AVFoundation之音频的播放和录制)