录音 - (Obj-C)

需要使用框架

使用步骤:
1.创建AVAudioRecorder对象

  • 设置录音生成文件的存放路径
  • 设置音频设定

2.准备录音 设置音轨,进行缓冲
3.开启录音

在实现录音的基础上,添加了一个功能,检测当前外界音量大小,如果当前的音量大小低于-40时,就自动停止录音
在实现基本的录音功能基础上:

$1. 开启音量的检测

self.recorder.meteringEnabled = YES;

$2.因为需要实时更新数据,所以要使用定时器,为了提高精度,使用了CADisplayLink,在定时器方法中调用更新音量

[self.recorder updateMeters];

$3.获取音量

CGFloat power = [self.recorder peakPowerForChannel:0];

$4.判断音量大小,是否停止录音

示例代码:

#import "ViewController.h"
#import 

@interface ViewController ()

// AVAudioRecorder对象
@property (nonatomic,strong) AVAudioRecorder *recorder;
// 定时器
@property (nonatomic,strong) CADisplayLink *displayLink;
// muteCounter
@property (nonatomic,assign) NSTimeInterval muteCounter;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 1. 创建AVAudioRecorder对象
    
    // 设置录音生成文件的存放路径
    NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"recorder.caf"];
    NSURL *fileUrl = [NSURL fileURLWithPath:libraryPath];
    
    /*   设置音频设定
     General Audio Format Settings:
         NSString *const AVFormatIDKey;             类型(手动设置音乐类型)
         NSString *const AVSampleRateKey;           采样率 单位:kHz(44.1kHz 标准的CD采样率,一秒震动441000次)
         NSString *const AVNumberOfChannelsKey;     声道(默认单声道)
     
     Linear PCM Format Settings:
         NSString *const AVLinearPCMBitDepthKey;    位深,用来分辨不同的声音 (16bit->2^16 可以支持65536种声音)
         NSString *const AVLinearPCMIsBigEndianKey;
         NSString *const AVLinearPCMIsFloatKey;
         NSString *const AVLinearPCMIsNonInterleaved;
     
     */
    NSDictionary *settings = @{AVSampleRateKey: @441000,AVNumberOfChannelsKey:@2};
    
    self.recorder = [[AVAudioRecorder alloc]initWithURL:fileUrl settings:settings error:nil];
    
    
    // 2. 准备录音 设置音轨,进行缓冲
    [self.recorder prepareToRecord];
    
    // 开启音量的检测
    self.recorder.meteringEnabled = YES;
    
}

// 开始录音
- (IBAction)startRecorderButtonClick:(id)sender {
    
    [self.recorder record];
    
    // 设置定时器,实时更新音量变化
    self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(updateMeters)];
    [self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
}
// 暂停录音
- (IBAction)pauserRecorderButtonClick:(id)sender {
    
    [self.recorder pause];
    
}
// 停止录音
- (IBAction)stopRecorderButtonClick:(id)sender {
    
    [self.recorder stop];
    
    [self.displayLink invalidate];
    
    self.displayLink = nil;

}

// 检测当前的音量大小
- (void)updateMeters{
    
    // 更新音量
    [self.recorder updateMeters];
    
    // 获取0声道的音量 (0 默认左声道)
    //  averagePowerForChannel获取的是平均值
    //  peakPowerForChannel 极值, 返回当时的分贝
    CGFloat power = [self.recorder peakPowerForChannel:0];
    // 返回值为负数,当为0时说明是最大值了,如果外界声音比较小,数值就会很小,当返回值为-160时代表停止
    NSLog(@"%f",power);
    
    
    // 假设当外界声音连续2s 低于-40时,自动停止录音
    if (power <= -40) {
        
        self.muteCounter ++;
        // CADisplayLink定时器方法默认大概一秒调用60次,所以当muteCounter自加到120时代表2s
        if (self.muteCounter >= 120) {
            
//            [self.recorder stop];  只是关闭录音,并没有关闭定时器
            [self stopRecorderButtonClick:nil];
            
        }
    } else {
        
        // 如果声音大小超过-40,计时归零
        self.muteCounter = 0;
    }
    
}


@end

你可能感兴趣的:(录音 - (Obj-C))