#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

ios录音的更多相关文章

  1. 【iOS 录音转码MP3及转码BASE64上传】

    iOS 录音转码MP3及转码BASE64上传 一,开始录音 NSLog(@"开始录音"); [self startRecord]; - (void)startRecord { // ...

  2. Swift实现iOS录音与播放音频功能

    作用AVPLayer:可以用来播放在线及本地音视频AVAudioSession:音频会话,主要用来管理音频设置与硬件交互使用时需要导入 #import <AVFoundation/AVFound ...

  3. lame,把ios录音转换为mp3格式

    在ios设备中进行录音,录音文件的格式为caf.但这种格式在很多设备中没法播放.为了适应终端的播放功能,特将caf转换为mp3格式文件来使用. 在录制caf文件时,需要使用双通道,否则在转换为MP3格 ...

  4. 【iOS录音与播放】实现利用音频队列,通过缓存进行对声音的采集与播放

    都说iOS最恶心的部分是流媒体,其中恶心的恶心之处更在即时语音. 所以我们先不谈即时语音,研究一下,iOS中声音采集与播放的实现. 要在iOS设备上实现录音和播放功能,苹果提供了简单的做法,那就是利用 ...

  5. ios录音、音频播放功能

    #import <AVFoundation/AVFoundation.h> { NSInteger   _timeCount;    NSTimer     *_timer; } @pro ...

  6. iOS录音加播放.

    现在发现的事实有: 如果没有蓝牙设备, 那么可以用下面的方法边录音, 边放音乐: 在录音按钮按下的时候: _avSession = [AVAudioSession sharedInstance];   ...

  7. ios 录音

    http://code4app.com/ios/%E5%BD%95%E9%9F%B3%E5%92%8C%E6%92%AD%E6%94%BE/51ba821b6803fa6901000000

  8. ios录音Demo

    <AudioToolbox/AudioToolbox.h> :这个库是C的接口,偏向于底层,主要用于在线流媒体的播放 <AVFoundation/AVFoundation.h> ...

  9. iOS录音后播放声音小,AudioSessionInitialize failed,AudioQueueStart failed (-50)

    方法1: UInt32 audioRoute = kAudioSessionOverrideAudioRoute_Speaker; AudioSessionSetProperty(kAudioSess ...

随机推荐

  1. struts2 action 3中书写方式

    1.pojo类   简单的javabean 类 需要有 public String execute(){return null;} 2.实现 Action 借口  重写 execute 3.继承 Ac ...

  2. flume与kafka整合

    flume与kafka整合 前提: flume安装和测试通过,可参考:http://www.cnblogs.com/rwxwsblog/p/5800300.html kafka安装和测试通过,可参考: ...

  3. 使用logrotate管理nginx日志文件

    本文转载自:http://linux008.blog.51cto.com/2837805/555829 描述:linux日志文件如果不定期清理,会填满整个磁盘.这样会很危险,因此日志管理是系统管理员日 ...

  4. java mail api 使用

    所需要的jar包: http://pan.baidu.com/s/1qWGZRJm 如果遇到这个错误:在windows防火墙允许 javaw.exe访问网络.或者关闭防火墙 FATAL ERROR i ...

  5. 细看INNODB数据落盘

    本文来自:沃趣科技 http://www.woqutech.com/?p=1459 1.  概述 前面很多大侠都分享过MySQL的InnoDB存储引擎将数据刷新的各种情况.我们这篇文章从InnoDB往 ...

  6. ABAP 仓库理货单导出

    *&---------------------------------------------------------------------* *& Report   *& ...

  7. Divide and conquer:4 Values whose Sum is 0(POJ 2785)

    找四个数的和为0 题目大意:给定四个集合,要你每个集合选4个数字,组成和为0 这题是3977的简单版,只要和是0就可以了 #include <iostream> #include < ...

  8. 表现层的设计(一)——常用的模式、Json与DTO

    上几篇博文介绍了 业务逻辑层和数据访问层,我认为写博文的作用主要是向业界的读者交流一种思想,点到为止,至于学习架构设计,通过几篇博文是讲不清楚的,还需要[基础]扎实的情况下,[反复]研究[权威]的书籍 ...

  9. 51nod 1116 K进制下的大数 (暴力枚举)

    题目链接 题意:中文题. 题解:暴力枚举. #include <iostream> #include <cstring> using namespace std; ; ; ch ...

  10. ajax+bootstrap做弹窗

    建页面,引入bootstrap弹窗 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " ...