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
ios录音的更多相关文章
- 【iOS 录音转码MP3及转码BASE64上传】
iOS 录音转码MP3及转码BASE64上传 一,开始录音 NSLog(@"开始录音"); [self startRecord]; - (void)startRecord { // ...
- Swift实现iOS录音与播放音频功能
作用AVPLayer:可以用来播放在线及本地音视频AVAudioSession:音频会话,主要用来管理音频设置与硬件交互使用时需要导入 #import <AVFoundation/AVFound ...
- lame,把ios录音转换为mp3格式
在ios设备中进行录音,录音文件的格式为caf.但这种格式在很多设备中没法播放.为了适应终端的播放功能,特将caf转换为mp3格式文件来使用. 在录制caf文件时,需要使用双通道,否则在转换为MP3格 ...
- 【iOS录音与播放】实现利用音频队列,通过缓存进行对声音的采集与播放
都说iOS最恶心的部分是流媒体,其中恶心的恶心之处更在即时语音. 所以我们先不谈即时语音,研究一下,iOS中声音采集与播放的实现. 要在iOS设备上实现录音和播放功能,苹果提供了简单的做法,那就是利用 ...
- ios录音、音频播放功能
#import <AVFoundation/AVFoundation.h> { NSInteger _timeCount; NSTimer *_timer; } @pro ...
- iOS录音加播放.
现在发现的事实有: 如果没有蓝牙设备, 那么可以用下面的方法边录音, 边放音乐: 在录音按钮按下的时候: _avSession = [AVAudioSession sharedInstance]; ...
- ios 录音
http://code4app.com/ios/%E5%BD%95%E9%9F%B3%E5%92%8C%E6%92%AD%E6%94%BE/51ba821b6803fa6901000000
- ios录音Demo
<AudioToolbox/AudioToolbox.h> :这个库是C的接口,偏向于底层,主要用于在线流媒体的播放 <AVFoundation/AVFoundation.h> ...
- iOS录音后播放声音小,AudioSessionInitialize failed,AudioQueueStart failed (-50)
方法1: UInt32 audioRoute = kAudioSessionOverrideAudioRoute_Speaker; AudioSessionSetProperty(kAudioSess ...
随机推荐
- mysql中表名是order的CRUD的错误
org.springframework.jdbc.BadSqlGrammarException: ### Error querying database. Cause: com.mysql.jdbc. ...
- CEF3开发者系列之进程间消息传递
在使用CEF3作为框架开发过程中,实现WebSockets.XMLHttpRequest.JS与本地客户端交互等功能时,需要在渲染(Render)进程和浏览(Browser)进程中传递消息.CEF3在 ...
- cxGrid 增加序号 (非数据库绑定模式) (测试通过)
cxGrid 增加序号 (非数据库绑定模式) ----------------------------------- 1. 选在 adoQuery 控件 , 鼠标右键菜单中 选择 Fields Edi ...
- Selenium WebDriver 处理table
首先,html table是由 table 元素以及一个或多个 tr.th 或 td 元素组成. for example: 这是一个简单的html table: 源码如下: <html> ...
- IOS-触摸事件
UITouch UITouch类中包含五个属性 •window:触摸产生时所处的窗口.由于窗口可能发生变化,当前所在的窗口不一定是最开始的窗口 •view:触摸产生时所处的视图.由于视图可能发生变化, ...
- 60. Permutation Sequence
题目: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of t ...
- mongodb3.x用户角色
用户和角色是多对多的关系,一个用户可以对应多个角色,一个角色可以拥有多个用户.用户角色的不同对应的权限也是不一样的.下面是一些分配给用户的常见的角色. read ...
- Android 天气曲线
参考:http://blog.csdn.net/qy274770068/article/details/51560148
- The Triangle
针对如下形式的ACM试题,大多出自南阳理工学院的在线ACM试题(网址: 南阳理工在线评测系统),在此非常感谢,同时也非常感谢作者的分享! 时间限制:1000 ms | 内存限制:65535 KB ...
- poj1308(简单并查集)
题目链接:http://poj.org/problem?id=1308 题意:x, y 表示x 与 y连接,给出一波这样的数据,问这组数据能否构成树,即不能形成回路,不能有多个根节点:要注意可以是空树 ...