<AudioToolbox/AudioToolbox.h> :这个库是C的接口,偏向于底层,主要用于在线流媒体的播放
<AVFoundation/AVFoundation.h> :提供了音频和回放的底层API,同时也负责管理音频的硬件
//需要遵循一个代理协议<AVAudioRecorderDelegate>
//
// ViewController.m
// 录音录频
//
// Created by lanou3g on 15/11/19.
// Copyright © 2015年 lanou3g. All rights reserved.
//
#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];
//2.设置录音的格式 / *在2000年被用在MPEG-4中(ISO 14496-3 Audio),所以现在变更为MPEG-4 AAC标准,也就是说,AAC已经成为MPEG4家族的主要成员之一,它是MPEG4第三部分中的音频编码系统。AAC可提供最多48个全音域音频通道。*/
[recorderSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
//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];
//5.线性采样位数 8 ,16 ,24 ,32、采样位数可以理解为声卡处理声音的解析度。这个数值越大,解析度就越高,录制和回放的声音就越真实 --一般都是16位的(2的16次方)/*
PCM的基本参数是采样频率和采样位深,采样频率就是每秒采样多少次,位深就是声音通过拾音器转成的电平信号被量化的精细度,同时也代表一次采样会用多少位保存
*/
[recorderSetting setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
//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];
}
- (void)btnDragUp:(UIButton *)sender{
[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)"]];