IOS 录音(AVAudioRecorder)
#import "HMViewController.h"
#import <AVFoundation/AVFoundation.h> @interface HMViewController ()
- (IBAction)startRecord;
- (IBAction)stopRecord;
@property (nonatomic, strong) AVAudioRecorder *recorder;
@property (nonatomic, strong) CADisplayLink *timer;
@property (nonatomic, strong) NSTimer *stopRecordTimer;
/** 静音的持续时间 */
@property (nonatomic, assign) CGFloat slientDuration;
@end @implementation HMViewController - (void)viewDidLoad
{
[super viewDidLoad]; } - (void)addTimer
{
self.timer = [CADisplayLink displayLinkWithTarget:self selector:@selector(update)];
[self.timer addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
} - (void)removeTimer
{
[self.timer invalidate];
self.timer = nil;
} - (void)update
{
// 更新测试值
[self.recorder updateMeters]; // 如果分贝不超过-20
float power = [self.recorder averagePowerForChannel:];
if (power <= -) { // 几乎为静音
self.slientDuration += self.timer.duration; if (self.slientDuration >= ) {
// 停止录音
[self.recorder stop];
}
} else { // 有说话
self.slientDuration = ; }
} //- (void)update
//{
// // 更新测试值
// [self.recorder updateMeters];
//
// // 如果分贝不超过-20
// float power = [self.recorder averagePowerForChannel:0];
// if (power <= -20) { // 几乎为静音
// if (!self.stopRecordTimer) {
// self.stopRecordTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self.recorder selector:@selector(stopRecord) userInfo:nil repeats:NO];
// }
// } else { // 有说话
//// [self.stopRecordTimer invalidate];
//// self.stopRecordTimer = nil;
// NSDate *time = [NSDate dateWithTimeIntervalSinceNow:2.0];
// [self.stopRecordTimer setFireDate:time];
// }
//} - (IBAction)startRecord {
NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [doc stringByAppendingPathComponent:@"test.caf"];
NSURL *url = [NSURL fileURLWithPath:path]; AVAudioRecorder *recorder = [[AVAudioRecorder alloc] initWithURL:url settings:nil error:nil];
// 缓冲
[recorder prepareToRecord];
// 开启分贝测量功能
recorder.meteringEnabled = YES;
// 开始录音
[recorder record];
self.recorder = recorder; // 开启定时器
[self addTimer];
} - (IBAction)stopRecord {
// [self.recorder stop];
}
@end
IOS 录音(AVAudioRecorder)的更多相关文章
- Domain=NSOSStatusErrorDomain Code=1937337955 关于iOS录音AVAudioRecorder与音频播放AVAudioPlayer真机调试录音不能播放的问题
error:Domain=NSOSStatusErrorDomain Code=1937337955 ,这个错误很常见, 原因是因为我们需要调用另外一个AVAudioPlayer 的初始化方法,来确定 ...
- 【iOS 录音转码MP3及转码BASE64上传】
iOS 录音转码MP3及转码BASE64上传 一,开始录音 NSLog(@"开始录音"); [self startRecord]; - (void)startRecord { // ...
- Swift实现iOS录音与播放音频功能
作用AVPLayer:可以用来播放在线及本地音视频AVAudioSession:音频会话,主要用来管理音频设置与硬件交互使用时需要导入 #import <AVFoundation/AVFound ...
- ios录音Demo
<AudioToolbox/AudioToolbox.h> :这个库是C的接口,偏向于底层,主要用于在线流媒体的播放 <AVFoundation/AVFoundation.h> ...
- iOS开发简记(4):录音AVAudioRecorder
录音,声音的采集,一般有两种实现办法,一是使用AVAudioRecorder,一是使用AudioUnit.如果只是简单的录音,使用AVAudioRecorder就可以了,如果想更灵活地处理刚录到的声音 ...
- iOS开发(4):录音AVAudioRecorder
录音,声音的采集,一般有两种实现办法,一是使用AVAudioRecorder,一是使用AudioUnit.如果只是简单的录音,使用AVAudioRecorder就可以了,如果想更灵活地处理刚录到的声音 ...
- 【iOS录音与播放】实现利用音频队列,通过缓存进行对声音的采集与播放
都说iOS最恶心的部分是流媒体,其中恶心的恶心之处更在即时语音. 所以我们先不谈即时语音,研究一下,iOS中声音采集与播放的实现. 要在iOS设备上实现录音和播放功能,苹果提供了简单的做法,那就是利用 ...
- ios录音、音频播放功能
#import <AVFoundation/AVFoundation.h> { NSInteger _timeCount; NSTimer *_timer; } @pro ...
- ios录音
#import "ViewController.h" #import <AVFoundation/AVFoundation.h> @interface ViewCont ...
随机推荐
- 多个git库的ssh配置
当拥有多个git服务器,而且都是用ssh认证方式时,需要在~/.ssh下的config文件做如下配置 如下:第一个为本地git库,第二个为github库,第三个为默认git库 Host 192.168 ...
- rm删除文件,空间没有释放
rm删除的文件,如果其他进程正在使用这个文件,那么文件句柄并没有释放 (df仍然会统计这个文件占用的空间) 此时只能重启这个进程 正确的删除文件方法应该是 >xxx.file (最好之前加个co ...
- vue filters过滤器的使用
说的很详细 https://www.w3cplus.com/vue/how-to-create-filters-in-vuejs.html
- Problem E: 积木积水 ——————【模拟】
Problem E: 积木积水 Description 现有一堆边长为1的已经放置好的积木,小明(对的,你没看错,的确是陪伴我们成长的那个小明)想知道当下雨天来时会有多少积水.小明又是如此地喜欢二次元 ...
- C++程序设计基础(6)内存分配
1.知识点 三步走:申请,释放,指针置空. 1.1malloc.free函数 在C语言中内存malloc函数申请动态空间,以下展示其基本用法: int *p = NULL; p = ();//申请 f ...
- PCU
PCU(Peak concurrent users ),互联网术语,应用在网络游戏和其他互联网服务领域,意思是最高同时在线人数 业务系统架构性能提升主要分为两种不同的方式,scale-out(横向扩展 ...
- VMWare启动虚拟机失败,提示锁定文件失败解决方法
1.问题描述:未正常关闭虚拟机,重新启动时,VMWare启动虚拟机失败 2.解决方法: ①找到该虚拟系统所在的目录,即弹出框中的目录,在目录中找到Windows XP Professional.vmx ...
- .net使用redis入门笔记
1.学习blog:http://www.cnblogs.com/yangecnu/p/Introduct-Redis-in-DotNET.html 2.redis官网:http://redis.io/ ...
- 关于C#的Lock锁思考
大家都知道多线程并发时候存在一个线程同步的问题,一般使用lock关键字来处理. lock关键字的结果如下: object locker=new object(); lock(locker) { ... ...
- [SQL SERVER系列]工作经常使用的SQL整理,实战篇(二)[原创]
工作经常使用的SQL整理,实战篇,地址一览: 工作经常使用的SQL整理,实战篇(一) 工作经常使用的SQL整理,实战篇(二) 工作经常使用的SQL整理,实战篇(三) 接着上一篇“工作经常使用的SQL整 ...