在iOS的基础类库中, 提供了AVFoundation FrameWork,即Audio/Video基础类库,通过使用这个类库,可以非常容易的在应用程序录制,播放视频,音频等。

在录制前要初始化 AVAudioRecorder *recorder;

//录音设置

NSMutableDictionary *recordSetting = [[[NSMutableDictionaryalloc]init] autorelease];

//设置录音格式 AVFormatIDKey==kAudioFormatLinearPCM

[recordSetting setValue:[NSNumbernumberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];

//设置录音采样率(Hz) 如:AVSampleRateKey==8000/44100/96000(影响音频的质量)

[recordSetting setValue:[NSNumbernumberWithFloat:44100] forKey:AVSampleRateKey];

//录音通道数 1 或 2

[recordSetting setValue:[NSNumbernumberWithInt:1] forKey:AVNumberOfChannelsKey];

//线性采样位数 8、16、24、32

[recordSetting setValue:[NSNumbernumberWithInt:16] forKey:AVLinearPCMBitDepthKey];

//录音的质量

[recordSetting setValue:[NSNumbernumberWithInt:AVAudioQualityHigh] forKey:AVEncoderAudioQualityKey];


NSString *strUrl = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

NSURL *url = [NSURLfileURLWithPath:[NSStringstringWithFormat:@"%@/lll.aac", strUrl]];

urlPlay = url;


NSError *error;

//初始化

recorder = [[AVAudioRecorderalloc]initWithURL:url settings:recordSetting error:&error];

//开启音量检测

recorder.meteringEnabled = YES;

recorder.delegate = self;


录制完后保存

- (IBAction)btnUp:(id)sender

{

double cTime = recorder.currentTime;

if (cTime > 2) {//如果录制时间<2 不发送

NSLog(@"发出去");

}else {

//删除记录的文件

[recorderdeleteRecording];

//删除存储的

}

[recorderstop];

[timerinvalidate];

}