#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. 实验二 PHP基本语法实验

    实验二 PHP基本语法实验 0 实验准备 0.1实验环境和相关工具软件 具体到的机房环境,请在Windowsxp环境下做本实验: l  操作系统:Windowsxp l  Web服务器:Apache ...

  2. ePass.CreateFile

    javascript和vbscript中没有结构体Struct,ePass的ActiveX对象中把各个参数都展开了,官方文档只给出了对应的代码,没有给出相应的数字,示例代码中却都是数字,其VC代码中有 ...

  3. 关于TxQBService报的错,腾讯你真牛B啊

    腾讯你真牛B啊,浏览器都7了,还特么的报这么低级的错误,还每10秒写一条windows日志,让人有什么心情用你的浏览器,滚.

  4. Effective C++ -----条款06:若不想使用编译器自动生成的函数,就该明确拒绝

    为驳回编译器自动提供的功能,可将相应的成员函数声明为private并且不予实现. 使用像Uncopyable这样的base class也是一种做法(即先声明一个基类,然后私有继承它).这其实有点像使用 ...

  5. DP:Cow Exhibition(POJ 2184)(二维问题转01背包)

        牛的展览会 题目大意:Bessie要选一些牛参加展览,这些牛有两个属性,funness和smartness,现在要你求出怎么选,可以使所有牛的smartness和funness的最大,并且这两 ...

  6. 【python】入门学习(四)

    函数: 定义函数 #area.py from math import pi def area(radius): """Return the area of a circl ...

  7. 字符串与byte数组转换

    string weclome=""; byte[] data = new byte[1024]; //字符串转byte数组 data = Encoding.ASCII.GetByt ...

  8. xcode 自定义Eclipse里边常用的快捷键

    之前在用Eclipse写Java的 时候,有几个常用的快捷键,比如删除当前行,在当前行下面插入空行,向上/下移动当前行等等,到了Xcode里怎么也找不到这些快捷键,一直觉得 Xcode自带的快捷键不够 ...

  9. [Android] 如何查看apk需要支持的Android版本

    reference to : http://blog.csdn.net/huiguixian/article/details/39928089 如果有一个apk,需要知道他最低安装支持的Android ...

  10. [Android] 解析android framework下利用app_process来调用java写的命令及示例

    reference to :http://bbs.9ria.com/thread-253058-1-1.html 在android SDK的framework/base/cmds目录下了,有不少目录, ...