#pragma mark - 开始录音---test
- (void)beginRecordByFileName:(NSString*)_fileName
{
//设置文件名和录音路径
self.recordFileName = _fileName;
self.recordFilePath = [VoiceRecorderBaseVCgetPathByFileName:self.recordFileNameofType:@"wav"];
NSError *error = [[NSError alloc] init];
//初始化录音
self.recorder = [[[AVAudioRecorderalloc] initWithURL:[NSURLfileURLWithPath:self.recordFilePath] settings:[VoiceRecorderBaseVCgetAudioRecorderSettingDict] error:&error]autorelease];
recorder.delegate = self;
recorder.meteringEnabled = YES;
[recorderprepareToRecord];
//开始录音
[[AVAudioSessionsharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecorderror:nil];
[[AVAudioSessionsharedInstance] setActive:YESerror:nil];
[recorder record];
}
#pragma mark - wavToAmr转换
- (IBAction)wav2Amr:(id)sender {
if (_originWav.length > 0){
self.convertAmr = [_originWavstringByAppendingString:@"wavToAmr"];
//转格式
NSFileManager* fm=[NSFileManagerdefaultManager];
NSData *wavdata = [fm contentsAtPath:[VoiceRecorderBaseVC getPathByFileName:_originWav ofType:@"wav"]];
NSString * wav = [NSString stringWithFormat:@"wavLength = %lu",(unsigned long)wavdata.length];
self.wavLabel.text = wav;
[VoiceConverterwavToAmr:[VoiceRecorderBaseVCgetPathByFileName:_originWavofType:@"wav"] amrSavePath:[VoiceRecorderBaseVCgetPathByFileName:_convertAmrofType:@"amr"]];
//发送音频数据 因为要播放无压缩音频,因此音频名字上传是原wav名,但上传的data数据是转换后的arm
NSData* data = [NSDatadataWithContentsOfFile:[VoiceRecorderBaseVCgetPathByFileName:_convertAmrofType:@"amr"]];
NSLog(@"amrlength = %lu",(unsigned long)data.length);
NSString * amr = [NSString stringWithFormat:@"amrlength = %lu",(unsigned long)data.length];
self.amrLabel.text = amr;
}
}
#pragma mark - 开始录音
- (IBAction)startButtonAction:(id)sender {
startdate = [NSDate date];
self.timeLabel.text = [NSStringstringWithFormat:@"%@",startdate];
//设置文件名
self.originWav = [VoiceRecorderBaseVCgetCurrentTimeString];
//开始录音
[selfbeginRecordByFileName:self.originWav];
}
#pragma mark - 停止录音
- (IBAction)stopButtonAction:(id)sender {
stopdate = [NSDate date];
self.stopTimeLabel.text = [NSStringstringWithFormat:@"%@",stopdate];
//停止录音
if (recorder.isRecording){
[recorder stop];
}
//回调录音文件路径
if ([self.vrbDelegate respondsToSelector:@selector(VoiceRecorderBaseVCRecordFinish:fileName:)]){
[self.vrbDelegateVoiceRecorderBaseVCRecordFinish:recordFilePathfileName:recordFileName];
}
}
#pragma mark - 播放录音
- (IBAction)playVoiceButtonAction:(id)sender {
if ([[NSFileManagerdefaultManager] fileExistsAtPath:[VoiceRecorderBaseVCgetPathByFileName:_originWavofType:@"wav"]]) {
player = [playerinitWithContentsOfURL:[NSURLfileURLWithPath:[VoiceRecorderBaseVCgetPathByFileName:_originWavofType:@"wav"]] error:nil];
if (![player isPlaying]) {
player.delegate = self;
[player play];
}
}
}