// ViewController.m
// 录音录频
//
#import "ViewController.h"
//提供了音频和回放的底层API,同时也负责管理音频硬件
#import <AVFoundation/AVFoundation.h>
//这个库是C的接口,偏向于底层,主要用于在线流媒体的播放
#import <AudioToolbox/AudioToolbox.h>
@interface ViewController () <AVAudioRecorderDelegate>
{
//用来录音
AVAudioRecorder *recorder;
//设置定时检测,用来监听当前音量大小,控制话筒图片
NSTimer *timer;
//设置一个路径,用来保存本地录音的路径
NSURL *urlPlay;
}
//用来控制录音功能
@property (nonatomic, strong) UIButton *btn;
//用来播放已经录好的音频文件
@property (nonatomic, strong) UIButton *playBtn;
//控制音量的图片
@property (nonatomic, strong) UIImageView *imageView;
//音频播放器
@property (nonatomic, strong) AVAudioPlayer *paly;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//基本步骤:1.进行录音设置(先配置录音机(是一个字典),设置录音的格式,录音的采样率,录音的先行采样位数,录音的通道数,录音质量,录音路径,初始化录音对象,开启音量检测);2.设置录音按钮的功能(UI设置)3.设置播放按钮并实现播放功能
//1.进行录音设置
[self audio];
//实现录音按钮的功能(UI设置)
//2.加图片
self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 100)/2, 100, 100, 100)];
self.imageView.backgroundColor = [UIColor redColor];
self.btn = [UIButton buttonWithType:UIButtonTypeCustom];
self.btn.frame = CGRectMake(self.imageView.frame.origin.x, 250, 50, 40); [self.btn setTitle:@"开始" forState:UIControlStateNormal]; [self.btn setBackgroundColor:[UIColor greenColor]]; self.playBtn = [UIButton buttonWithType:UIButtonTypeCustom]; self.playBtn.frame = CGRectMake(self.imageView.frame.origin.x + 50, 250, 50, 40); [self.playBtn setTitle:@"播放" forState:UIControlStateNormal]; [self.playBtn setBackgroundColor:[UIColor cyanColor]]; [self.playBtn addTarget:self action:@selector(paly:) forControlEvents:UIControlEventTouchUpInside]; //设置按钮的事件,当按钮被按下时 [self.btn addTarget:self action:@selector(btnDown:) forControlEvents:UIControlEventTouchDown]; //当手指抬起时 [self.btn addTarget:self action:@selector(btnUp:) forControlEvents:UIControlEventTouchUpInside]; //当触摸拖动离开控制范围时 [self.btn addTarget:self action:@selector(btnDragUp:) forControlEvents:UIControlEventTouchDragExit]; [self.view addSubview:self.playBtn]; [self.view addSubview:self.btn]; [self.view addSubview:self.imageView]; }//点击播放按钮的实现播放- (void)paly:(UIButton *)sender{ //加载本地录音文件 self.paly = [[AVAudioPlayer alloc] initWithContentsOfURL:urlPlay error:nil]; //准备播放 [self.paly prepareToPlay]; //播放 [self.paly play]; }//录音设置的方法- (void)audio{ //1.先配置Recorder(录音机) NSMutableDictionary *recorderSetting = [NSMutableDictionary dictionary];
//3.设置录音采样率 --采样频率是指录音设备在一秒钟内对声音信号的采样次数,采样频率越高声音的还原就越真实越自然。在当今的主流声卡上,采样频率一般共分为22.05KHz、44.1KHz、48KHz三个等级,22.05只能达到FM广播的声音品质,44.1KHz则是理论上的CD音质界限,48KHz则更加精确一些
[recorderSetting setValue:[NSNumber numberWithFloat:44100] forKey:AVSampleRateKey];
//4.设置录音的通道数
[recorderSetting setValue:[NSNumber numberWithInt:1] forKey:AVNumberOfChannelsKey];
PCM的基本参数是采样频率和采样位深,采样频率就是每秒采样多少次,位深就是声音通过拾音器转成的电平信号被量化的精细度,同时也代表一次采样会用多少位保存
//6,录音质量
[recorderSetting setValue:[NSNumber numberWithInt:AVAudioQualityHigh] forKey:AVEncoderAudioQualityKey];
//设置录音路径
NSString *strUrl = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/record.aac",strUrl]];
//记录当前路径
urlPlay = url;
//初始化录音对象
NSError *error;
recorder = [[AVAudioRecorder alloc] initWithURL:url settings:recorderSetting error:&error];
//开启音量检测
recorder.meteringEnabled = YES;
recorder.delegate = self;
}
- (void)btnDown:(UIButton *)sender
{
[sender setTitle:@"停止" forState:UIControlStateNormal];
//创建录音文件,准备录音
if([recorder prepareToRecord]){
//开始
[recorder record];
}
//设置定时检测,检测我们的图片
timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(detectionVioce) userInfo:nil repeats:YES];
}
- (void)btnUp:(UIButton *)sender{
//UI状态
[sender setTitle:@"开始" forState:UIControlStateNormal];
//记录当前时间
double cTime = recorder.currentTime;
if (cTime > 2) {
NSLog(@"放出去");
}else{
//删除我们的记录文件
[recorder deleteRecording];
}
//停止录音
[recorder stop];
//停止计时器
[timer invalidate];
}
[sender setTitle:@"开始" forState:UIControlStateNormal];
//删除录制文件
[recorder deleteRecording];
//停止录音
[recorder stop];
//停止计时器
[timer invalidate];
NSLog(@"取消发送");
}
//检测当前声音
- (void)detectionVioce
{
//刷新当前音量数据
[recorder updateMeters];
//获取音量的平均值
double lowPassResults = pow(10, (0.05 * [recorder peakPowerForChannel:0]));//×于当前音量
//现在取值范围在0~1 (每隔0.7)
if(0 < lowPassResults < 0.06){
[self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(1)"]];
}else if( 0.06< lowPassResults <= 0.13){
[self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(2)"]];
}else if( 0.13<lowPassResults <= 0.20){
[self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(3)"]];
}else if( 0.20 <lowPassResults <= 0.27){
[self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(4)"]];
}else if( 0.27 <lowPassResults <= 0.34){
[self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(5)"]];
}else if( 0.34 <lowPassResults <= 0.41){
[self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(6)"]];
}else if( 0.41 <lowPassResults <= 0.48){
[self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(7)"]];
}。。。。。){ [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(8)"]]; }else if( 0.55 <lowPassResults <= 0.62){ [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(9)"]]; }else if( 0.62 <lowPassResults <= 0.69){ [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(10)"]]; }){ [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(8)"]]; }else if( 0.55 <lowPassResults <= 0.62){ [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(9)"]]; }else if( 0.62 <lowPassResults <= 0.69){ [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(10)"]]; }else if( 0.69 <lowPassResults <= 0.76){ [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(11)"]]; }else if( 0.76 <lowPassResults <= 0.83){ [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(12)"]]; }else if( 0.83 <lowPassResults <= 0.90){ [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(13)"]]; }else{ [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(14)"]]; }else if( 0.48 <lowPassResults <= 0.55){ [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(8)"]]; }else if( 0.55 <lowPassResults <= 0.62){ [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(9)"]]; }else if( 0.62 <lowPassResults <= 0.69){ [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(10)"]]; }else if( 0.69 <lowPassResults <= 0.76){ [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(11)"]]; }else if( 0.76 <lowPassResults <= 0.83){ [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(12)"]]; }else if( 0.83 <lowPassResults <= 0.90){ [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(13)"]]; }else{ [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(14)"]]; }else if( 0.48 <lowPassResults <= 0.55){ [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(8)"]]; }else if( 0.55 <lowPassResults <= 0.62){ [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(9)"]]; }else if( 0.62 <lowPassResults <= 0.69){ [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(10)"]]; }else if( 0.69 <lowPassResults <= 0.76){ [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(11)"]]; }else if( 0.76 <lowPassResults <= 0.83){ [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(12)"]]; }else if( 0.83 <lowPassResults <= 0.90){ [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(13)"]]; }else{ [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(14)"]]; }else if( 0.48 <lowPassResults <= 0.55){ [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(8)"]]; }else if( 0.55 <lowPassResults <= 0.62){ [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(9)"]]; }else if( 0.62 <lowPassResults <= 0.69){ [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(10)"]]; }else if( 0.69 <lowPassResults <= 0.76){ [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(11)"]]; }else if( 0.76 <lowPassResults <= 0.83){ [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(12)"]]; }else if( 0.83 <lowPassResults <= 0.90){ [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(13)"]]; }else{ [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(14)"]]; }){ [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(8)"]]; }else if( 0.55 <lowPassResults <= 0.62){ [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(9)"]]; }else if( 0.62 <lowPassResults <= 0.69){ [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(10)"]]; }else if( 0.69 <lowPassResults <= 0.76){ [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(11)"]]; }else if( 0.76 <lowPassResults <= 0.83){ [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(12)"]]; }else if( 0.83 <lowPassResults <= 0.90){ [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(13)"]]; }else{ [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(14)"]]; }0.69){ [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(10)"]]; }else if( 0.69 <lowPassResults <= 0.76){ [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(11)"]]; }else if( 0.76 <lowPassResults <= 0.83){ [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(12)"]]; }else if( 0.83 <lowPassResults <= 0.90){ [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(13)"]]; }else{ [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(14)"]];
ios录音Demo的更多相关文章
-
【iOS 录音转码MP3及转码BASE64上传】
iOS 录音转码MP3及转码BASE64上传 一,开始录音 NSLog(@"开始录音"); [self startRecord]; - (void)startRecord { // ...
-
91平台iOS接入demo
源码:http://pan.baidu.com/s/1DuBl6 今天整理硬盘,找到了一个有趣的demo.一年前,91助手游戏联运呈爆棚趋势,但是许多使用FlashAir开发的优秀的游戏和应用都卡在了 ...
-
Swift实现iOS录音与播放音频功能
作用AVPLayer:可以用来播放在线及本地音视频AVAudioSession:音频会话,主要用来管理音频设置与硬件交互使用时需要导入 #import <AVFoundation/AVFound ...
-
【iOS录音与播放】实现利用音频队列,通过缓存进行对声音的采集与播放
都说iOS最恶心的部分是流媒体,其中恶心的恶心之处更在即时语音. 所以我们先不谈即时语音,研究一下,iOS中声音采集与播放的实现. 要在iOS设备上实现录音和播放功能,苹果提供了简单的做法,那就是利用 ...
-
lame,把ios录音转换为mp3格式
在ios设备中进行录音,录音文件的格式为caf.但这种格式在很多设备中没法播放.为了适应终端的播放功能,特将caf转换为mp3格式文件来使用. 在录制caf文件时,需要使用双通道,否则在转换为MP3格 ...
-
03.WebView演练-iOS开发Demo(示例程序)源代码
技术博客http://www.cnblogs.com/ChenYilong/ 新浪微博http://weibo.com/luohanchenyilong //转载请注明出处--本文永久链接:h ...
-
XMPP协议实现即时通讯底层书写 (二)-- IOS XMPPFramework Demo+分析
我希望,This is a new day! 在看代码之前,我认为你还是应该先整理一下心情,来听我说几句: 首先,我希望你是在早上边看这篇blog,然后一边開始动手操作,假设你仅仅是看blog而不去自 ...
-
适合新人学习的iOS官方Demo
UICatalog.包括了绝大部分经常使用的UI,入门必备良药. 9 分段选择器 10滑动条 Slider 11stack view 12 分步条 13 开关 14 textfield 15text ...
-
iOS商城demo、音乐播放器、视频通话、自定义搜索、转场动画等源码
iOS精选源码 微信自定义搜索框实现 一个商城Demo,持续更新中 在Object-C中学习数据结构与算法之排序算法 iOS 音乐播放器之锁屏歌词+歌词解析+锁屏效果 XLsn0wPushTimePi ...
随机推荐
-
免费真机调试 -- Xcode7以上版本
刚新安装了Xcode7 , 据说这个版本可以免费真机调试,于是用了一个新的AppID测试了,发现真的可以免费真机调试了呢!新的appId账号(随便一个苹果手机账号就行),没有支付每年的99美刀,也没有 ...
-
java reflect 例子
public static void main(String[] args) { Student stu1 = new Student(); stu1.setId(1); stu1.setName(& ...
-
将秒格式化为时分秒的JS函数
//时间格式化 function GetDateTime(time) { if (time >= 60 && time <= 3600) { time = parseInt ...
-
【原创】一起学C++ 之enum ---------C++ primer plus(第6版)
枚举 定义:在默认情况下讲整数值赋给枚举量,第一个枚举量的值为0,第二个枚举量的值为1,依次+1 一.定义一个枚举,枚举类型,枚举量 *与C#相比个人认为C++的enum不好一点是不能通过枚举名点其中 ...
-
【CSS学习笔记】背景图片
直接看代码: background: blue; /*页面背景为蓝色,很简单的代码*/background-image:url(small.jpg); /*页面背景是名字叫small.jpg的图片*/ ...
-
handsontable 事件汇总
Hook插件 afterChange (changes: Array, source: String):1个或多个单元格的值被改变后调用 changes:是一个2维数组包含row,prop,oldVa ...
-
java并发编程——通过ReentrantLock,Condition实现银行存取款
java.util.concurrent.locks包为锁和等待条件提供一个框架的接口和类,它不同于内置同步和监视器.该框架允许更灵活地使用锁和条件,但以更难用的语法为代价. Lock 接口 ...
-
软件推荐----数据库数据处理Navicat Premium
一般情况下,企业对数据库的管控很严,通常管控分成以下: 对整台服务器管理权限,如:操作系统Administrator+SA 对单个数据库(Oracle的表空间)完全权限,如:SQL DB_Owner ...
-
sahrepoint 上传到文档库
sharepoint学习笔记汇总 http://blog.csdn.net/qq873113580/article/details/20390149 /// <summary&g ...
-
【js与jquery】jquery循环滚动新闻
2.html代码: <h3>最新动态</h3> <div class="scrollNews" > <ul> <li>& ...