iOS--AVAudioRecorder如何录制MP3

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ZuoWeiXiaoDuZuoZuo/article/details/83068382

AVAudioRecorder简介

录音机,提供了在应用程序中的音频记录能力。作为与 AVAudioPlayer 相对应的 API,AVAudioRecorder 是将音频录制为文件的最简单的方法。除了用一个音量计接受音量的峰值和平均值以外,这个 API 简单粗暴,如果你的使用场景很简单的话,这可能恰恰就是你想要的方法。

录音配置

创建AVAudioSession

想要录音首先要创建一个AVAudioSession,这个通道用来调用录音设备;


    AVAudioSession *session = [AVAudioSession sharedInstance];
    
    NSError *sessionError;
    
    [session setCategory:AVAudioSessionCategoryPlayAndRecord error:&sessionError];
    
    if(session == nil){
        
        NSLog(@"Error creating session: %@", [sessionError description]);
        
    }
    else{
        
        [session setActive:YES error:nil];
        
    }

        

创建录音设置字典

想要录音 总得配置点啥吧

    NSMutableDictionary *audioSetting = [NSMutableDictionary dictionary]; 
    
    // 设置录音格式 kAudioFormatMPEGLayer3设置貌似是没用的 默认设置就行
    //[audioSetting setObject:@(kAudioFormatMPEGLayer3) forKey:AVFormatIDKey];
    
    // 设置录音采样率,8000 44100 96000,对于一般录音已经够了
    [audioSetting setObject:@(22150) forKey:AVSampleRateKey];
    
    // 设置通道 1 2
    [audioSetting setObject:@(1) forKey:AVNumberOfChannelsKey];
    
    // 每个采样点位数,分为8、16、24、32
    [audioSetting setObject:@(16) forKey:AVLinearPCMBitDepthKey];
    
    // 是否使用浮点数采样 如果不是MP3需要用Lame转码为mp3的一定记得设置NO!(不然转码之后的声音一直都是杂音)
    // 是否使用浮点数采样 如果不是MP3需要用Lame转码为mp3的一定记得设置NO!(不然转码之后的声音一直都是杂音)
    // 是否使用浮点数采样 如果不是MP3需要用Lame转码为mp3的一定记得设置NO!(不然转码之后的声音一直都是杂音)
    
    [audioSetting setObject:@(YES) forKey:AVLinearPCMIsFloatKey];
    
    // 录音质量
    [audioSetting setObject:@(AVAudioQualityHigh) forKey:AVEncoderAudioQualityKey];
    

创建一个录音文件的存放路径

录完音总得给个地方存吧

    //  在Documents目录下创建一个名为FileData的文件夹
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject] stringByAppendingPathComponent:@"Cache/AudioData"];
    NSLog(@"%@",path);
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isDir = FALSE;
    BOOL isDirExist = [fileManager fileExistsAtPath:path isDirectory:&isDir];
    if(!(isDirExist && isDir)) {
        BOOL bCreateDir = [fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
        if(!bCreateDir){
            NSLog(@"创建文件夹失败!");
        }
        NSLog(@"创建文件夹成功,文件路径%@",path);
    }
    
     //每次启动后都保存一个新的文件中
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    [formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]];
    
    [formatter setDateFormat:@"yyyy_MM_dd_HH_mm_ss"];
   
    NSString *dateStr = [formatter stringFromDate:[NSDate date]];
    
    // 想要录制MP3格式的 这里 MP3 必须大写 !!!!(苹果的所有后缀名都是大写,所以这是个坑)
    // 想要录制MP3格式的 这里 MP3 必须大写 !!!!(苹果的所有后缀名都是大写,所以这是个坑)
    // 想要录制MP3格式的 这里 MP3 必须大写 !!!!(苹果的所有后缀名都是大写,所以这是个坑)
    
    path = [path stringByAppendingFormat:@"/%@.MP3",dateStr];
    
    NSLog(@"file path:%@",path);
    
    NSURL *url=[NSURL fileURLWithPath:path];
    

录音完整操作

    //如果是在录音就不做动作
    if ([self.audioRecorder isRecording]) {
        return;
    }
    
    //创建录音通道
    [self setAudioSession];
    
    //创建录音格式设置
    NSDictionary *setting = [self getAudioSetting];
    
    //创建录音文件保存路径
    NSURL *url = [self getSavePath];
    
    //创建录音机
    NSError *error=nil;
    self.audioRecorder=[[AVAudioRecorder alloc]initWithURL:url settings:setting error:&error];
    self.audioRecorder.delegate=self;
    self.audioRecorder.meteringEnabled=YES;//如果要监控声波则必须设置为YES
    
    if (error) {
        NSLog(@"创建录音机时发生错误,信息:%@",error.localizedDescription);
    }
    else{
        if (![self.audioRecorder isRecording]) {
            
            NSLog(@"录音开始");
            
            [self.audioRecorder record];
            
        }
    }

相关链接

猜你喜欢

转载自blog.csdn.net/ZuoWeiXiaoDuZuoZuo/article/details/83068382
mp3