#pragma mark - 开始录制,暂停、撤销 功能
//MUtilities方法
+ (NSString*) getAudioFileDateTimeString
{
NSDateFormatter* dateFormater= [[NSDateFormatteralloc] init];
[dateFormater setDateFormat: @"yyyy-MM-dd-HH-mm-ss"];
//NSMutableString* str = [NSMutableString new];
NSString* ret = [dateFormaterstringFromDate:[NSDatedate]];
[retretain];
[dateFormaterrelease];
NSString* str = [[NSStringalloc ] initWithFormat:@"%@.m4a",ret];
NSLog(@"file name %@ ",str);
return [strautorelease];
}
-(void)beginRecord
{
NSString *documentDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0] ;
//定义音频文件的路径和名字
NSString* doc = [NSStringstringWithFormat:@"%@/%@/%@/%@",documentDir,self.userName,self.userName,[MUtilitiesgetAudioFileDateTimeString]];
audioFilePath = doc;
//定义一个音频录制格式的字典
NSMutableDictionary *recordSetting = [[NSMutableDictionaryalloc] init];
[recordSetting setValue :[NSNumbernumberWithInt:kAudioFormatMPEG4AAC]forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumbernumberWithFloat:16000.0]forKey:AVSampleRateKey];
[recordSetting setValue :[NSNumbernumberWithInt:16]forKey:AVLinearPCMBitDepthKey];
[recordSetting setValue :[NSNumbernumberWithBool:NO]forKey:AVLinearPCMIsBigEndianKey];
[recordSettingsetValue :[NSNumbernumberWithBool:NO]forKey:AVLinearPCMIsFloatKey];//
[recordSetting setValue:[NSNumbernumberWithInt:2]forKey:AVNumberOfChannelsKey];//声道
NSURL *fileURL=[[NSURLalloc]initFileURLWithPath:doc];
NSError * error;
self.recorder=[[AVAudioRecorderalloc]initWithURL:fileURLsettings:recordSetting error:&error];
//最长录音时间
[self.recorderrecordForDuration:(NSTimeInterval)4.0 * 60.0];
NSError * error1;
AVAudioSession *session = [AVAudioSessionsharedInstance];
[session setCategory:AVAudioSessionCategoryRecorderror:&error1];
[self.recorderprepareToRecord];
[self.recordersetDelegate:self];
[self.recorderrecord];
}
#pragma mark - 取消录音
- (void)clickAudioCancel: (id) sender
{
[selfremoveAudio];
}
#pragma mark - 停止录音
-(void)stopRecord
{
if(self.recorder.recording)
{
[self.recorderstop];
}
}