zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    NGUI Sprite 和 Label 改变Layer 或父物体后 未更新深度问题
    unity销毁层级物体及 NGUI 深度理解总结
    unity 2d 和 NGUI layer
    关于NGUI与原生2D混用相互遮盖的问题心得
    CentOS7为firewalld添加开放端口及相关操作
    Python 操作redis有序集合(sorted set)
    win10下安装redis 服务
    python2/3中 将base64数据写成图片,并将图片数据转为16进制数据的方法、bytes/string的区别
    解决最小化安装Centos7后无法上网的问题,以及安装成功后的基本配置
    在mysql中使用group by和order by取每个分组中日期最大一行数据,亲测有效
  • 原文地址:https://www.cnblogs.com/tangranyang/p/4662925.html
Copyright © 2011-2022 走看看