#import "RecordViewController.h"
#import <AVFoundation/AVFoundation.h>
#define kRecordAudioFile @"myRecord.caf"
@interface RecordViewController ()
@property (nonatomic,strong) AVAudioRecorder *audioRecorder;//音频录音机
@property (nonatomic,strong) AVAudioPlayer *audioPlayer;//音频播放器,用于播放录音文件
@property (nonatomic,strong) NSTimer *timer;//录音声波监控(注意这里暂时不对播放进行监控)
@property (strong, nonatomic) UIButton *record;//开始录音
@property (strong, nonatomic) UIButton *pause;//暂停录音
@property (strong, nonatomic) UIButton *resume;//恢复录音
@property (strong, nonatomic) UIButton *stop;//停止录音
@property (strong, nonatomic) UIProgressView *audioPower;//音频波动
@end
@implementation RecordViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self createSub];
[self setAudioSession];
}
- (void)createSub {
//录音
self.record = [UIButton buttonWithType:UIButtonTypeSystem];
self.record.frame = CGRectMake(10, 500, 60, 40);
self.record.backgroundColor = [UIColor orangeColor];
[self.record setTitle:@"录音" forState:UIControlStateNormal];
[self.record addTarget:self action:@selector(record:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.record];
//暂停
self.pause = [UIButton buttonWithType:UIButtonTypeSystem];
self.pause.frame = CGRectMake(80, 500, 60, 40);
self.pause.backgroundColor = [UIColor orangeColor];
[self.pause setTitle:@"暂停" forState:UIControlStateNormal];
[self.pause addTarget:self action:@selector(pause:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.pause];
//回复
self.resume = [UIButton buttonWithType:UIButtonTypeSystem];
self.resume.frame = CGRectMake(150, 500, 60, 40);
self.resume.backgroundColor = [UIColor orangeColor];
[self.resume setTitle:@"恢复" forState:UIControlStateNormal];
[self.resume addTarget:self action:@selector(resume:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.resume];
//停止
self.stop = [UIButton buttonWithType:UIButtonTypeSystem];
self.stop.frame = CGRectMake(220, 500, 60, 40);
self.stop.backgroundColor = [UIColor orangeColor];
[self.stop setTitle:@"停止" forState:UIControlStateNormal];
[self.stop addTarget:self action:@selector(stop:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.stop];
self.audioPower = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
self.audioPower.frame = CGRectMake(20, 200, 100, 5);
self.audioPower.progressTintColor = [UIColor orangeColor];
self.audioPower.trackTintColor = [UIColor blueColor];
[self.view addSubview:self.audioPower];
}
#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 *)getAudioSetting{
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 getAudioSetting];
//创建录音机
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 lowPassResults = pow(10, (0.05 * [self.audioRecorder peakPowerForChannel:0]));
NSLog(@"%.2f", lowPassResults);
CGFloat pro = 0;
if (0<lowPassResults<=0.27) {
pro = 0;
}else if (0.27<lowPassResults<=0.34) {
pro = 0.2;
}else if (0.34<lowPassResults<=0.41) {
pro = 0.4;
}else if (0.41<lowPassResults<=0.48) {
pro = 0.6;
}else if (0.48<lowPassResults<=0.55) {
pro = 0.8;
}else if (0.55<lowPassResults) {
pro = 1;
}
[self.audioPower setProgress:pro];
}
#pragma mark - UI事件
/**
* 点击录音按钮
*
* @param button 录音按钮
*/
- (void)record:(UIButton *)button {
if (![self.audioRecorder isRecording]) {
[self.audioRecorder record];//首次使用应用时如果调用record方法会询问用户是否允许使用麦克风
self.timer.fireDate = [NSDate distantPast];
}
}
- (void)pause:(UIButton *)bu {
if ([self.audioRecorder isRecording]) {
[self.audioRecorder pause];
self.timer.fireDate=[NSDate distantFuture];
}
}
- (void)resume:(UIButton *)bu {
[self record:bu];
}
- (void)stop:(UIButton *)bu {
[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(@"sdf");
}
NSLog(@"录音完成!");
}
网友评论