#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 配置 struts.xml 提示

    1.这个提示通常是在 连网络的时候才可以看到 2.当没有网路的时候我们该如何配置呢? window -->preferences -->xml catelog -->user.... ...

  2. fib数列变种题目

    对一个正整数作如下操作:如果是偶数则除以2,如果是奇数则加1,如此进行直到1时操作停止,求经过9次操作变为1的数有多少个? 第9次操作:结果1由2产生.1个被操作数8:结果2只能由4产生.1个被操作数 ...

  3. Unity3d 检查哪些prefab引用了某个UIAtlas

    适用情景:策划在用NGUI制作UI prefab时经常会使用一些临时的Atlas,然后再想改就不知道都哪些使用了.现在想修改下使用临时资源的GameObject 使用方式,先选中某个prefab或者某 ...

  4. mysql日常语句总结

    #删除mysql的二进制日志文件 #将删除mysql-bin.*****1之前的日志文件 purge binary logs to 'mysql-bin.*****1'; #重新生成一个二进制日志文件 ...

  5. 6.js模式-中介者模式

    1. 中介者模式 所有对象通过中介者进行通信 var playDirector = (function(){ var players = []; var options = {}; options.a ...

  6. Android仿快递 物流时间轴 的代码实现

    首先,这篇参考了别人的代码.根据自己的项目需求简单改造了一下,效果图如下 xml:代码 <?xml version="1.0" encoding="utf-8&qu ...

  7. 【HOG】

    http://blog.csdn.net/masibuaa/article/details/12917961 把这份资料大概看完了 大概了解Hog了

  8. 【mysql】Blob类型

    来源:http://qgyang.blog.sohu.com/115847378.html 一般在需要存储较大数据时使用Bolb MySql的Bolb四种类型 MySQL中,BLOB是一个二进制大型对 ...

  9. 关于Javascript splice方法的一个坑。

    w3c相关文档:http://www.w3school.com.cn/jsref/jsref_splice.asp bug:购物车计算价格的时候.加商品没问题,减商品的时候价格总是计算错误. 经排查发 ...

  10. 第一次点击Div1显示Div2,再次点击Div1的时候又隐藏Div2

    要使用Jquery来实现,记得引用Jquery库哦,代码如下: $(document).ready(function(){ $("#ck1").click(function(){ ...