//
// ViewController.m
// 录音
//
// Created by cjw on 16/7/11.
// Copyright © 2016年 ch. All rights reserved.
//
#import "ViewController.h"
#import
#define kRecordAudioFile @"myRecord.caf"
@interface ViewController ()<AVAudioRecorderDelegate>
@property (nonatomic,strong)AVAudioRecorder * audioRecorder;//音频录音机
@property (nonatomic,strong)AVAudioPlayer * audioPlayer;//音频播放器,用于播放录音
@property (nonatomic,strong)NSTimer * timer;//录音声波监控;
@property (weak, nonatomic) IBOutlet UIButton *record;
@property (weak, nonatomic) IBOutlet UIButton *pause;
@property (weak, nonatomic) IBOutlet UIButton *resume;
@property (weak, nonatomic) IBOutlet UIButton *stop;
@property (weak, nonatomic) IBOutlet UIProgressView *audioPower;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setAudioSession];
}
#pragma mark - 私有方法
/**
* 设置音频会话
*
*/
-(void)setAudioSession
{
AVAudioSession * audioSession = [AVAudioSession sharedInstance];
//设置为播放和录制状态,以便在录制完成之后播放录音
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
[audioSession setActive:YES error:nil];
}
/**
* 取得录音文件的保存路径
*
* @return 录音文件的路径
*/
-(NSURL *)getSavePath
{
NSString * urlStr = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES ) lastObject];
urlStr = [urlStr stringByAppendingPathComponent:kRecordAudioFile];
NSLog(@"file:path:%@",urlStr);
NSURL * url = [NSURL fileURLWithPath:urlStr];
return url;
}
/**
* 取得录音文件的设置
*
* @return 录音设置
*/
-(NSDictionary *)getAudioSettion
{
NSMutableDictionary * dicM = [NSMutableDictionary dictionary];
//设置录音格式
[dicM setObject:@(kAudioFormatLinearPCM) forKey:AVFormatIDKey];
//设置录音采样率,8000是电话采样率,对于一般的录音已经够了
[dicM setObject:@(8000) forKey:AVSampleRateKey];
//设置通道,这里采用单声道
[dicM setObject:@(1) forKey:AVNumberOfChannelsKey];
//每个采样点位数,分为8,16,24,32
[dicM setObject:@(8) forKey:AVLinearPCMBitDepthKey];
//是否使用浮点数采样
[dicM setObject:@(YES) forKey:AVLinearPCMIsFloatKey];
//。。。。是他设置
return dicM;
}
/**
* 获得录音机对象
*
* @return 录音机对象
*/
-(AVAudioRecorder * )audioRecorder
{
if (!_audioRecorder) {
//创建录音文件保存路径
NSURL * url =[self getSavePath];
//创建录音格式设置
NSDictionary * setting = [self getAudioSettion];
//创建录音机
NSError * error = nil;
_audioRecorder = [[AVAudioRecorder alloc]initWithURL:url settings:setting error:&error];
_audioRecorder.delegate = self;
_audioRecorder.meteringEnabled = YES;//如果要控制声波则必须设置为YES
if(error)
{
NSLog(@"创建录音机对象发生错误,错误信息是:%@",error.localizedDescription);
return nil;
}
}
return _audioRecorder;
}
/**
* 创建播放器
*
* @return 播放器
*/
-(AVAudioPlayer *)audioPlayer
{
if (!_audioPlayer) {
NSURL * url = [self getSavePath];
NSError * error = nil;
_audioPlayer =[[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error];
_audioPlayer.numberOfLoops = 0;
[_audioPlayer prepareToPlay];
if (error) {
NSLog(@"创建播放器过程出错:错误信息是:%@",error.localizedDescription);
return nil;
}
}
return _audioPlayer;
}
/**
* 录音声波监控定时器
*
* @return 定时器
*/
-(NSTimer * )timer
{
if (!_timer) {
_timer = [NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(audioPowerChange) userInfo:nil repeats:YES];
}
return _timer;
}
-(void)audioPowerChange
{
[self.audioRecorder updateMeters];//跟新检测值
float power = [self.audioRecorder averagePowerForChannel:0];//获取第一个通道的音频,注音音频的强度方位-160到0
CGFloat progerss = (1.0/160)*(power+160);
[self.audioPower setProgress:progerss];
}
#pragma mark - 按钮点击事件
/**
* 点击录音按钮
*
* @param sender 录音按钮
*/
- (IBAction)recordClick:(id)sender {
if (![self.audioRecorder isRecording]) {
[self.audioRecorder record];
self.timer.fireDate = [NSDate distantPast];
}
}
/**
* 点击暂停按钮
*
* @param sender 暂停按钮
*/
- (IBAction)pauseClick:(id)sender {
if ([self.audioRecorder isRecording]) {
[self.audioRecorder pause];\
self.timer.fireDate=[NSDate distantFuture];
}
}
/**
* 点击恢复按钮
* 恢复录音只需要再次调用record,AVAudioSession会帮助你记录上次录音位置并追加录音
*
* @param sender 恢复按钮
*/
- (IBAction)resumeClick:(id)sender {
[self recordClick:sender];
}
/**
* 点击停止按钮
*
* @param sender 停止按钮
*/
- (IBAction)stopClick:(id)sender {
[self.audioRecorder stop];
self.timer.fireDate = [NSDate distantFuture];
self.audioPower.progress = 0.0;
}
#pragma mark - 录音机代理方法
/**
* 录音完成,录音完成后播放录音
*
* @param recorder 录音机对象
* @param flag 是否成功
*/
-(void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag{
if (![self.audioPlayer isPlaying]) {
[self.audioPlayer play];
}
NSLog(@"录音完成!");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end