AVAudioRecorder 录音

苹果提供了AVAudioRecorder类来让我们进行音频录制,是录音变的非常的便捷!
在使用AVAudioRecorder进行录音之前要做几样准备工作,
① 、在iOS10及以上,调用系统功能需要在info.plist文件里添加相应的key,如图,在info.plist文件下添加Key: Privacy - Microphone Usage Description, Value值可以自己随便写哦!


Key

② 、进行权限验证,如下代码:

AVAudioSession *session = [AVAudioSession sharedInstance];
 if ([session respondsToSelector:@selector(requestRecordPermission:)]) {

        [session performSelector:@selector(requestRecordPermission:) withObject:^(BOOL granted) {
            if (granted) {

                // 用户同意获取麦克风,一定要在主线程中执行UI操作!!!
                dispatch_queue_t queueOne = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
                dispatch_async(queueOne, ^{

                    dispatch_async(dispatch_get_main_queue(), ^{

                        //在主线程中执行UI,这里主要是执行录音和计时的UI操作
                        [self record];
                    });
                });
            } else {

                // 用户不同意获取麦克风
                UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"麦克风不可用" message:@"请在“设置 - 隐私 - 麦克风”中允许XXX访问你的麦克风" preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *openAction = [UIAlertAction actionWithTitle:@"前往开启" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

                    //如果要让用户直接跳转到设置界面,则可以进行下面的操作,如不需要,就忽略下面的代码
                    /*
                     *iOS10 开始苹果禁止应用直接跳转到系统单个设置页面,只能跳转到应用所有设置页面
                     *iOS10以下可以添加单个设置的系统路径,并在info里添加URL Type,将URL schemes 设置路径为prefs即可。
                     *@"prefs:root=Sounds"
                     */
                    NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];

                    if([[UIApplication sharedApplication] canOpenURL:url]) {

                        [[UIApplication sharedApplication] openURL:url];
                    }
                }];

                [alertController addAction:openAction];
                [self presentViewController:alertController animated:YES completion:nil];
            }
        }];
    }

3️⃣ 、为了知道你正在录音,肯定得搞个录音的计时器吧!
首先搞个label显示时间

- (void)addTimerLabel {

    //这个是自己封装的创建label的方法!
    _timerLabel = [CommonLib createLabelFrame:CGRectMake(20, 120, kScreenWidth - 40, TimerLabelH) text:@"00:00:00" alignment:NSTextAlignmentCenter textColor:[UIColor greenColor] font:TimerLabelH];
    [self.view addSubview:_timerLabel];
}

//要计时当然少不了计时器了,这里计时器设置成每秒更新,因为这里设定的最小时间单位是秒,若最小时间单位是毫秒,就把1.0,改成0.01!这个方法在你点击录音的时候调用
- (void)startTimer {

    _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateSecond:) userInfo:nil repeats:YES];
}

//执行更新UI的操作,每秒执行
- (void)updateSecond:(NSTimer *)timer {

    _second ++;
    if (_second == 1) {

        [self enbleBtn];
    }
    //这个方法是把时间显示成时分秒的形式显示在label上
    NSString *timerStr = [self convertTimeToString:_second];
    _timerLabel.text = timerStr;
}

 //规范时间格式
- (NSString *)convertTimeToString:(NSInteger)second {

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"HH:mm:ss"];
    NSDate *date = [formatter dateFromString:@"00:00:00"];
    date = [date dateByAddingTimeInterval:second];
    NSString *timeString = [formatter stringFromDate:date];
    return timeString;
}

好了,以上步骤走完,就可以开始录音的操作了!

//设置后台播放,下面这段是录音和播放录音的设置
_session = [AVAudioSession sharedInstance];
NSError *sessionError;
[_session setCategory:AVAudioSessionCategoryPlayAndRecord error:&sessionError];
//判断后台有没有播放
if (_session == nil) {

        SIMEILog(@"Error creating sessing:%@", [sessionError description]);
} else {
//关闭其他音频播放,把自己设为活跃状态
        [_session setActive:YES error:nil];
}

if (![_timer isValid]) {

            [self startTimer];
} else {

            //这个方法是写了一个NSTimer的拓展类 Category,具体方法在下面附上代码
            [_timer continueTimer];
}

//设置AVAudioRecorder
if (!self.recorder) {

            NSDictionary *settings = @{AVFormatIDKey  :  @(kAudioFormatLinearPCM), AVSampleRateKey : @(11025.0), AVNumberOfChannelsKey :@2, AVEncoderBitDepthHintKey : @16, AVEncoderAudioQualityKey : @(AVAudioQualityHigh)};

            //开始录音,将所获取到得录音存到文件里 _recordUrl
是存放录音的文件路径,在下面附上
            self.recorder = [[AVAudioRecorder alloc] initWithURL:_recordUrl settings:settings error:nil];

            /*
             * settings 参数
             1.AVNumberOfChannelsKey 通道数 通常为双声道 值2
             2.AVSampleRateKey 采样率 单位HZ 通常设置成44100 也就是44.1k,采样率必须要设为11025才能使转化成mp3格式后不会失真
             3.AVLinearPCMBitDepthKey 比特率 8 16 24 32
             4.AVEncoderAudioQualityKey 声音质量
             ① AVAudioQualityMin  = 0, 最小的质量
             ② AVAudioQualityLow  = 0x20, 比较低的质量
             ③ AVAudioQualityMedium = 0x40, 中间的质量
             ④ AVAudioQualityHigh  = 0x60,高的质量
             ⑤ AVAudioQualityMax  = 0x7F 最好的质量
             5.AVEncoderBitRateKey 音频编码的比特率 单位Kbps 传输的速率 一般设置128000 也就是128kbps

             */
        }

        //准备记录录音
        [_recorder prepareToRecord];

        //开启仪表计数功能,必须开启这个功能,才能检测音频值
        [_recorder setMeteringEnabled:YES];
        //启动或者恢复记录的录音文件
        [_recorder record];

下面附上NSTimer的Category,怎么创建Category呢,右击New Files -> ObjectC - Files -> File Type选择Category, Class 选择NSTimer ,File文件名自己随便取

在.h文件中添加两个方法

//暂停计时
- (void)pauseTimer;

//继续计时
- (void)continueTimer;

在.m文件中实现两个方法

- (void)pauseTimer {

    //如果已被释放则return!isValid对应invalidate
    if (![self isValid]) return;
    //启动时间为很久以后
    [self setFireDate:[NSDate distantFuture]];
}

- (void)continueTimer {

    if (![self isValid]) return;
    //启动时间为现在
    [self setFireDate:[NSDate date]];
}

附上_recordUrl的路径创建

//因为录音文件比较大,所以我们把它存在Temp文件里,Temp文件里的文件在app重启的时候会自动删除
_recordFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent: @"record.caf"];
    //创建临时文件来存放录音文件
_recordUrl = [NSURL fileURLWithPath:self.recordFilePath];

下面我们就要把录音文件转成mp3格式了,在网上查了很多方法,发现大部分人都推荐使用lame来转换mp3!
所以下面附上lame转换mp3的方法,lame的github下载链接https://github.com/rbrito/lame

下载完成后倒入过程中,在Build Phases 下的Link Binary With Library 下添加lame


lame


一般导入后Library Search Paths 就会自动添加上lame文件在工程中的路径,若没有添加上,就自己手动添加上,否则会报错!!!

导入成功后,就开始使用lame转换mp3了,一大段的C代码,C语言好的同学看着就比较得心应手了

首先#import "lame.h"

//然后开始写转换的代码
    //如果要保存下来自己听,就讲mp3保存到到Document中,如下代码,如果不的话,就保存到Temp文件中
    NSString *fileName = [NSString stringWithFormat:@"/%@.mp3", nowTime];
    NSString *filePath = [[NSHomeDirectory() stringByAppendingFormat:@"/Documents/"] stringByAppendingPathComponent:fileName];
    _mp3Url = [NSURL URLWithString:filePath];

    @try {
        int read, write;

        FILE *pcm = fopen([_recordFilePath cStringUsingEncoding:1], "rb");   //source 被转换的音频文件位置
        fseek(pcm, 4 * 1024, SEEK_CUR);//删除头,否则在前一秒钟会有杂音
        FILE *mp3 = fopen([filePath cStringUsingEncoding:1], "wb"); //output 输出生成的Mp3文件位置

        const int PCM_SIZE = 8192;
        const int MP3_SIZE = 8192;
        short int pcm_buffer[PCM_SIZE * 2];
        unsigned char mp3_buffer[MP3_SIZE];
        //这里要注意,lame的配置要跟AVAudioRecorder的配置一致,否则会造成转换不成功
        lame_t lame = lame_init();
        lame_set_in_samplerate(lame, 11025.0);//采样率
        lame_set_VBR(lame, vbr_default);
        lame_init_params(lame);

        do {
            read = (int)fread(pcm_buffer, 2 * sizeof(short int), PCM_SIZE, pcm);
            if (read == 0)
                write = lame_encode_flush(lame, mp3_buffer, MP3_SIZE);
            else
                write = lame_encode_buffer_interleaved(lame, pcm_buffer, read, mp3_buffer, MP3_SIZE);

            fwrite(mp3_buffer, write, 1, mp3);

        } while (read != 0);

        lame_close(lame);
        fclose(mp3);
        fclose(pcm);
    }
    @catch (NSException *exception) {
        NSLog(@"%@",[exception description]);
    }
    @finally {
        NSLog(@"MP3生成成功!!!");
}

好了以上就是录音和转mp3的主要代码了,最后说一下,这个demo上传github的时候出了点问题,所以如果要demo的同学把邮箱留一下哈!若是对你有帮助的话,随手点个赞哦

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值