iOS 实现录音功能

参考资料

https://www.jianshu.com/p/fb7dfb033989

音频文件相关知识

文件格式
wav:
特点:音质最好的格式,对应PCM编码
适用:多媒体开发,保存音乐和音效素材
mp3:
特点:音质好,压缩比比较高,被大量软件和硬件支持
适用:适合用于比较高要求的音乐欣赏
caf:
特点:适用于几乎iOS中所有的编码格式
编码格式
PCM
PCM:脉冲编码调制,是一种非压缩音频数字化技术,是一种未压缩的原音重现,数字模式下,音频的初始化信号是PCM
MP3
AAC
AAC:其实是“高级音频编码(advanced audio coding)”的缩写,他是被设计用来取代MPC格式的。
HE-AAC
HE-AAC是AAC的一个超集,这个“High efficiency”,HE-AAC是专门为低比特率所优化的一种音频编码格式。
AMR
AMR全称是“Adaptive Multi-Rate”,它也是另一个专门为“说话(speech)”所优化的编码格式,也是适合低比特率环境下采用。
ALAC
它全称是“Apple Lossless”,这是一种没有任何质量损失的音频编码方式,也就是我们说的无损压缩。
IMA4
IMA4:这是一个在16-bit音频文件下按照4:1的压缩比来进行压缩的格式。
影响音频文件大小的因素
采样频率
采样频率是指单位时间内的采样次数。采样频率越大,采样点之间的间隔就越小,数字化后得到的声音就越逼真,但相应的数据量就越大。
采样位数
采样位数是记录每次采样值数值大小的位数。采样位数通常有8bits或16bits两种,采样位数越大,所能记录声音的变化度就越细腻,相应的数据量就越大。
声道数
声道数是指处理的声音是单声道还是立体声。单声道在声音处理过程中只有单数据流,而立体声则需要左、右声道的两个数据流。显然,立体声的效果要好,但相应的数据量要比单声道的数据量加倍。
时长
计算
数据量(字节/秒)=(采样频率(Hz)× 采样位数(bit)× 声道数)/ 8
总大小=数据量 x 时长(秒)
设置申请访问权限
申请访问权限,在plist文件中加入
<key>NSMicrophoneUsageDescription</key> <string>App需要您的同意,才能访问麦克风</string>
实测音频大小
录音1分钟:
caf格式用了2.6MB
mp3格式用了227KB 

录音10分钟:
caf格式用了26.5MB
mp3格式用了2.3MB 
caf转mp3

参考我的这篇博客
https://www.jianshu.com/p/62cac1ddb2a5

代码实现

#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>

@interface ViewController ()<AVAudioRecorderDelegate,AVAudioPlayerDelegate>{
    
    AVAudioRecorder *recorder;
    AVAudioPlayer *player;
    /** 录音计时器 */
    NSTimer *recordTimer;
    /** 播放计时器 */
    NSTimer *playTimer;
    /** 录音时间 */
    NSInteger recordSecond;
    /** 录音分钟时间 */
    NSInteger minuteRecord;
    /** 播放时间 */
    NSInteger playSecond;
    /** 播放分钟时间 */
    NSInteger minutePlay;
    /** caf文件路径 */
    NSURL *tmpUrl;
}

/** 时间 */
@property (nonatomic, strong) UILabel *timeLbl;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self createUI];
}

#pragma mark - 搭建界面
- (void)createUI{
    
    // 开始,这里自己创建UIButton
    UIButton *startBtn = [UIButton createButtonWithTitle:@"开始" sel:@selector(startBtnEvent:) vc:self];
    startBtn.frame = CGRectMake(100, 100, 200, 50);
    [self.view addSubview:startBtn];

    // 结束
    UIButton *endBtn = [UIButton createButtonWithTitle:@"结束" sel:@selector(endBtnEvent:) vc:self];
    endBtn.frame = CGRectMake(100, 150, 200, 50);
    [self.view addSubview:endBtn];

    // 播放
    UIButton *playBtn = [UIButton createButtonWithTitle:@"播放" sel:@selector(playBtnEvent:) vc:self];
    playBtn.frame = CGRectMake(100, 200, 200, 50);
    [self.view addSubview:playBtn];
    
    // 时间
    self.timeLbl = [[UILabel alloc] initWithFrame:CGRectMake(100, 250, 200, 50)];
    self.timeLbl.textColor = [UIColor blackColor];
    self.timeLbl.textAlignment = NSTextAlignmentCenter;
    self.timeLbl.text = @"00:00";
    [self.view addSubview:self.timeLbl];
}


/**
 开始
 */
- (void)startBtnEvent:(UIButton *)btn{

    // 开始录音
    [self recordingAction];
}


/**
 结束
 */
- (void)endBtnEvent:(UIButton *)btn{

    // 停止录音
    [self stopAction];
}


/**
 播放
 */
- (void)playBtnEvent:(UIButton *)btn{

   // 播放录音
   [self playAction];
}


/**
 开始录音
 */
- (void)recordingAction {
    
    NSLog(@"开始录音");
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    [audioSession setCategory:AVAudioSessionCategoryRecord error:nil];

    //录音设置
    NSMutableDictionary *recordSettings = [[NSMutableDictionary alloc] init];
    //录音格式 
    [recordSettings setValue :[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey: AVFormatIDKey];
    //采样率
    [recordSettings setValue :[NSNumber numberWithFloat:11025.0] forKey: AVSampleRateKey];
    //通道数
    [recordSettings setValue :[NSNumber numberWithInt:2] forKey: AVNumberOfChannelsKey];
    //线性采样位数
    [recordSettings setValue :[NSNumber numberWithInt:16] forKey: AVLinearPCMBitDepthKey];
    //音频质量,采样质量
    [recordSettings setValue:[NSNumber numberWithInt:AVAudioQualityMin] forKey:AVEncoderAudioQualityKey];

    NSError *error = nil;
    // 沙盒目录Documents地址
    NSString *recordUrl = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    // caf文件路径
    tmpUrl = [NSURL URLWithString:[recordUrl stringByAppendingPathComponent:@"selfRecord.caf"]];
    recorder = [[AVAudioRecorder alloc]initWithURL:tmpUrl settings:recordSettings error:&error];
    
    if (recorder) {
        //启动或者恢复记录的录音文件
        if ([recorder prepareToRecord] == YES) {
            [recorder record];

            recordSecond = 0;
            minuteRecord = 0;
            recordTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(recordSecondChange) userInfo:nil repeats:YES];
            [recordTimer fire];
        }
        
    }else {
        NSLog(@"录音创建失败");
    }
}


/**
 录音计时
 */
- (void)recordSecondChange {
    
    recordSecond ++;
    if (recordSecond > 59) {
        
        minuteRecord ++;
        recordSecond = 0;
    }
    self.timeLbl.text = [NSString stringWithFormat:@"%.2ld:%.2ld",(long)minuteRecord,(long)recordSecond];
}


/**
 停止录音
 */
- (void)stopAction {
    
    NSLog(@"停止录音");
    //停止录音
    [recorder stop];
    recorder = nil;
    [recordTimer invalidate];
    recordTimer = nil;
    
    self.timeLbl.text = [NSString stringWithFormat:@"%.2ld:%.2ld",(long)minuteRecord,(long)recordSecond];
}


/**
 播放录音
 */
- (void)playAction {
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
    NSError *playError;
    
    player = [[AVAudioPlayer alloc] initWithContentsOfURL:tmpUrl error:&playError];
    //当播放录音为空, 打印错误信息
    if (player == nil) {
        NSLog(@"Error crenting player: %@", [playError description]);
    }else {
        player.delegate = self;
        NSLog(@"开始播放");
        //开始播放
        playSecond = recordSecond;
        minutePlay = minuteRecord;
        if ([player prepareToPlay] == YES) {
           
            [player play];
            playTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(playSecondChange) userInfo:nil repeats:YES];
            [playTimer fire];
        }
    }
}


/**
 播放计时
 */
- (void)playSecondChange {
    playSecond --;

    if (playSecond < 0) {
        
        if (minutePlay <= 0) {
            
            playSecond = 0;
            minutePlay = 0;
            [playTimer invalidate];
        }else{
            minutePlay --;
            playSecond = 59;
        }
        
    }
    self.timeLbl.text = [NSString stringWithFormat:@"%.2ld:%.2ld",(long)minutePlay,(long)playSecond];
}


//当播放结束后调用这个方法
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
    NSLog(@"播放结束");
    [playTimer invalidate];
    playTimer = nil;
    
    self.timeLbl.text = [NSString stringWithFormat:@"%.2ld:%.2ld",(long)minuteRecord,(long)recordSecond];
}
@end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值