#pragma mark -录音设置配置
- (void)initRecordConfig{
//录音设置
NSMutableDictionary*recordSetting = [[NSMutableDictionaryalloc]init];
//设置录音格式AVFormatIDKey==kAudioFormatLinearPCM
[recordSettingsetValue:[NSNumbernumberWithInt:kAudioFormatLinearPCM]forKey:AVFormatIDKey];
//设置录音采样率(Hz)如:AVSampleRateKey==8000/44100/96000(影响音频的质量),采样率必须要设为11025才能使转化成mp3格式后不会失真
[recordSettingsetValue:[NSNumbernumberWithFloat:11025.0]forKey:AVSampleRateKey];
//录音通道数1或2,要转换成mp3格式必须为双通道
[recordSettingsetValue:[NSNumbernumberWithInt:2]forKey:AVNumberOfChannelsKey];
//线性采样位数8、16、24、32
[recordSettingsetValue:[NSNumbernumberWithInt:16]forKey:AVLinearPCMBitDepthKey];
//录音的质量
[recordSettingsetValue:[NSNumbernumberWithInt:AVAudioQualityHigh]forKey:AVEncoderAudioQualityKey];
//存储录音文件
_recordUrl= [NSURLURLWithString:[NSTemporaryDirectory()stringByAppendingString:@"selfRecord.caf"]];
//初始化
_audioRecorder= [[AVAudioRecorderalloc]initWithURL:_recordUrlsettings:recordSettingerror:nil];
//开启音量检测
_audioRecorder.meteringEnabled=YES;
_audioRecorder.delegate=self;
}
录音长按按钮
#pragma mark- UILongPressGestureRecognizer
//添加手势操作,长按按钮
- (void)handSpeakBtnPressed:(UILongPressGestureRecognizer*)gestureRecognizer {
UIView*view= gestureRecognizer.view;
UIImageView*imageView = (UIImageView*)view.superview;
if(gestureRecognizer.state==UIGestureRecognizerStateBegan) {
if(self.typeIndex==2||self.typeIndex==4) {
[selfplayVoiceBtnAction];
return;
}
[imageViewstartAnimating];
[selfstartRecordVoice];
self.remindLabel.text=@"松开结束";
}
if(gestureRecognizer.state==UIGestureRecognizerStateEnded) {
[imageViewstopAnimating];
[selfstopRecordVoice];
self.remindLabel.text=@"按住说话";
}
}
#pragma mark --------开始录音
- (void)startRecordVoice{
[self.sendkBtnsetUserInteractionEnabled:NO];
_audioSession= [AVAudioSessionsharedInstance];//得到AVAudioSession单例对象
[self.audioSessionsetCategory:AVAudioSessionCategoryPlayAndRecorderror:nil];//设置类别,表示该应用同时支持播放和录音
[self.audioSessionsetActive:YESerror:nil];//启动音频会话管理,此时会阻断后台音乐的播放.
[self.audioRecorderprepareToRecord];
[self.audioRecorderpeakPowerForChannel:1.0];
[self.audioRecorderrecord];
self.second=0;
self.timer= [NSTimerscheduledTimerWithTimeInterval:1target:selfselector:@selector(timeAnim)userInfo:nilrepeats:YES];
}
#pragma mark -定时器
- (void)timeAnim{
if(self.second>=TOTAL_SECOND){
[selfstopRecordVoice];
[self.timerinvalidate];
}
self.second++;
if(self.second<10) {
self.timeLabel.textColor= [UIColorcolorWithHexString:@"818C9E"];
self.timeLabel.text= [NSStringstringWithFormat:@"0:0%ld",(long)self.second];
}else{
self.timeLabel.textColor= [UIColorcolorWithHexString:@"818C9E"];
self.timeLabel.text= [NSStringstringWithFormat:@"0:%ld",(long)self.second];
}
}
#pragma mark --------停止录音
- (void)stopRecordVoice{
if(self.typeIndex!=1) {
return;
}
[self.recordBtnImageViewstopAnimating];
if(self.second<5) {
self.timeLabel.text=@"0:00";
[self.timerinvalidate];
[self.audioRecorderstop];//录音停止
[self.audioSessionsetActive:NOerror:nil];//一定要在录音停止以后再关闭音频会话管理(否则会报错),此时会延续后台音乐播放
self.timeLabel.textColor= [UIColorcolorWithHexString:@"E6E8EB"];
[MBProgressHUDshowError:@"说话时间不得低于5秒"toView:self.view];
}else{
[selftransformCAFToMP3];
[self.timerinvalidate];
[self.audioRecorderstop];//录音停止
[self.audioSessionsetCategory:AVAudioSessionCategoryPlaybackerror:nil];//此处需要恢复设置回放标志,否则会导致其它播放声音也会变小
[self.audioSessionsetActive:NOerror:nil];//一定要在录音停止以后再关闭音频会话管理(否则会报错),此时会延续后台音乐播放
if(self.playerView==nil){
NSString*videoStr = [NSTemporaryDirectory()stringByAppendingPathComponent:@"myselfRecord.mp3"];
NSURL*fileURL = [NSURLfileURLWithPath:videoStr];
AVURLAsset*asset = [AVURLAssetURLAssetWithURL:fileURLoptions:nil];
//录音完的时候调用,显示音波图
self.playerView= [[SYWaveformPlayerViewalloc]initWithFrame:CGRectMake(0,116,self.view.frame.size.width,82)asset:assetcolor:[UIColorlightGrayColor]progressColor:[UIColorcolorWithHexString:@"818C9E"]];
[self.WaveformViewaddSubview:self.playerView];
}
self.timeLabel.font= [UIFontsystemFontOfSize:32];
[self.timeLabelsetFont:[UIFontfontWithName:@"Helvetica-Bold"size:32]];
self.timeLabel.frame=CGRectMake(0,221,WIDTH,32);
self.remindLabel.hidden=YES;
self.typeIndex=2;
self.recordBtnImageView.image=kImageWithName(@"btn_recording_play");
//基于现有的一个值,再进行平移
self.sendkBtn.hidden=NO;
//基于现有的一个值,再进行平移
self.delectVoiceBtn.hidden=NO;
self.delectVoiceBtn.alpha=0;
self.sendkBtn.alpha=0;
[UIViewanimateWithDuration:0.5animations:^{
self.delectVoiceBtn.transform=CGAffineTransformTranslate(self.delectVoiceBtn.transform, -(WIDTH/2-55),0);
self.sendkBtn.transform=CGAffineTransformTranslate(self.sendkBtn.transform,WIDTH/2-55,0);
self.delectVoiceBtn.alpha=1;
self.sendkBtn.alpha=1;
}];
}
}