ios录音

 

#import "ViewController.h"

#import <AVFoundation/AVFoundation.h>

 

@interface ViewController ()

 

@property(nonatomic,strong)AVAudioRecorder*record;

@property(nonatomic,strong)CADisplayLink *updatereflesh;

@property(nonatomic,assign)CGFloat timePress;

 

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    //添加两个按钮

    UIButton *playBtn=[UIButton buttonWithType:UIButtonTypeCustom];

    playBtn.backgroundColor=[UIColor greenColor];

    playBtn.frame=CGRectMake(10, 40, 100, 50);

    [self.view addSubview:playBtn];

    [playBtn addTarget:self action:@selector(playMusic) forControlEvents:UIControlEventTouchUpInside];

    

    UIButton *pauseBtn=[UIButton buttonWithType:UIButtonTypeCustom];

    pauseBtn.backgroundColor=[UIColor redColor];

    pauseBtn.frame=CGRectMake(10, 100, 100, 50);

    [self.view addSubview:pauseBtn];

    [pauseBtn addTarget:self action:@selector(pause) forControlEvents:UIControlEventTouchUpInside];

    

    //沉默两秒自动停止录音

    

    //获取沙盒路径保存文件

    NSString *path=[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"123.caf"];

    NSURL *url=[NSURL fileURLWithPath:path];

    

    //录音

    //    //settings  设置参数  录音相关参数  声道  速率  采样率

    //    NSMutableDictionary *setting = [NSMutableDictionary dictionary];

    //    //2.够着  录音参数

    //    // 音频格式

    //    setting[AVFormatIDKey] = @(kAudioFormatAppleIMA4);

    //    // 音频采样率

    //    setting[AVSampleRateKey] = @(8000.0);

    //    // 音频通道数

    //    setting[AVNumberOfChannelsKey] = @(1);

    //    // 线性音频的位深度

    //    setting[AVLinearPCMBitDepthKey] = @(8);

 

    

    

    AVAudioRecorder *recorder=[[AVAudioRecorder alloc]initWithURL:url settings:nil error:NULL];

    self.record=recorder;

    //允许监听

    self.record.meteringEnabled=YES;

    

    

    

}

 

//开始播放

-(void)playMusic

{

    NSLog(@"开始录音");

    [self.record record];

    //开启定时器

    [self.updatereflesh addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

}

//暂停

-(void)pause

{

     NSLog(@"结束录音");

    [self.record stop];

    NSLog(@"%@",NSHomeDirectory());

}

//创建定时器

-(CADisplayLink*)updatereflesh

{

    if(!_updatereflesh)

    {

        _updatereflesh=[CADisplayLink displayLinkWithTarget:self selector:@selector(updateTimeAudioPinLv)];

    }

    return _updatereflesh;

}

-(void)updateTimeAudioPinLv

{

    //时时更新分贝

    [self.record updateMeters];

    //获取当前分贝

    CGFloat power=[self.record averagePowerForChannel:1];

    //开始沉默记录时间

    if(power<-30)

    {

        _timePress+=1.0/60;

        if (_timePress>2.0) {

            [self.record stop];

            //定时器停止

            [self.updatereflesh invalidate];

            self.updatereflesh =nil;

        }

    }

    else

    {

        _timePress=0;

    }

}

 

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

@end

原文地址:https://www.cnblogs.com/tangranyang/p/4662925.html