1.导框架
1.系统框架
CoreAudio.framework
AVFoundation.framework
2.导头文件
#import
3.遵守协议
{
AVAudioRecorder *_recoder; //录音
}
2.录音
//record
//第一个参数:录音存放位置的字符串转化为的网址
NSString *path = NSHomeDirectory();
//用当前日期的字符串作为文件名
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
formatter.dateFormat = @"yyyyMMddHHmmss";
NSString *dateStr = [formatter stringFromDate:date];
NSString *path1 = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"/Documents/%@.caf",dateStr]];
NSLog(@"path1 = %@",path1);
path = [path stringByAppendingPathComponent:@"/Documents/1.caf"];
NSLog(@"path = %@",path);
NSURL *url = [NSURL fileURLWithPath:path];
//第二个参数:录音相关设置
NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
//1.设置录音格式:AVFormatIDKey
[dict setObject:[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey];
//2.设置录音采样速率:AVSampleRateKey 8000/44100/96000
[dict setObject:[NSNumber numberWithInt:44100] forKey:AVSampleRateKey];
//3.设置录音质量:AVEncoderAudioQualityKey
[dict setObject:[NSNumber numberWithInt:AVAudioQualityLow] forKey:AVEncoderAudioQualityKey];
//4.设置线性采样位数:AVLinearPCMBitDepthKey 8/16/24
[dict setObject:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
//5.录音通道:AVNumberOfChannelsKey 1/2
[dict setObject:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey];
AVAudioSession *session = [[AVAudioSession alloc]init];
[session setCategory:AVAudioSessionCategoryRecord error:nil];
[session setActive:YES error:nil];
if (!_recoder) {
_recoder = [[AVAudioRecorder alloc]initWithURL:url settings:dict error:nil];
}
_recoder.delegate = self;
if ([_recoder prepareToRecord]) {
[_recoder record];
}
//pause
[_recoder pause];
//stop
[_recoder stop];
3.协议
AVAudioRecorderDelegate相关
-(void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag{
NSLog(@"finish");
}
-(void)audioRecorderEncodeErrorDidOccur:(AVAudioRecorder *)recorder error:(NSError *)error{
NSLog(@"error:%@",error);
}
-(void)audioRecorderBeginInterruption:(AVAudioRecorder *)recorder{
NSLog(@"系统级别的中断开始");
}
-(void)audioRecorderEndInterruption:(AVAudioRecorder *)recorder withOptions:(NSUInteger)flags{
NSLog(@"系统级别的中断结束");
//决定中断结束后,是否要恢复录音/播放
if (flags == AVAudioSessionInterruptionOptionShouldResume) {
[_recoder record];
}
//恢复录音或播放
}