原生音视频编码 https://github.com/loyinglin/LearnVideoToolBox/tree/mastergit
基于 AVFoundation 进行音频录制github
@interface ViewController ()<AVAudioRecorderDelegate> { AVAudioRecorder *recorder; AVAudioPlayer *player; } @property (weak, nonatomic) IBOutlet UILabel *label; @end - (IBAction)record:(id)sender { if (recorder == nil) { // 路径拼接 NSString *filePath = [NSString stringWithFormat:@"%@/rec_audio.caf", [self documentsDirectory]]; // 构建URL NSURL *fileUrl = [NSURL fileURLWithPath:filePath]; NSError *error = nil; // AVAudioSession类提供了Audio Session服务,Audio Session是指定应用于音频系统如何交互。AVAudioSession经过指定一个音频类别实现的,音频类别描述了应用使用音频的方式 // 下面是设置音频回话类别 // AVAudioSessionCategoryRecord表明只能输入音频,即录制音频,其效果是中止其余音频播放,开始录制音频, AVAudioSessionCategoryPlayback表明只能输出音频,即进行音频播放。 [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:&error]; // 设置是否“活跃”,这会把系统的任何声音关闭 [[AVAudioSession sharedInstance] setActive:YES error:&error]; // 设置settings参数 NSMutableDictionary *settings = [NSMutableDictionary dictionary]; // AVFormatIDKey键是设置录制音频编码格式,kAudioFormatLinearPCM表明线性PCM编码格式,PCM(pulse code modulation)线性脉冲编码调制,它是一种非压缩格式。 // 注意:编码格式与文件格式不一样,例如WAV是音频文件格式,它采用线性PCM音频编码 [settings setValue:[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey]; // AVSampleRateKey键是设置音频采样频率,44100.0是音频CD,VCD,SVCD和MP3所用的采样频率。 [settings setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey]; // AVNumberOfChannelsKey设置声道数量,值为1或者2 [settings setValue:[NSNumber numberWithInt:1] forKey:AVNumberOfChannelsKey]; // AVLinearPCMBitDepthKey这是采样位数,取值为8,16,24,32,16是默认值 [settings setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey]; // AVLinearPCMIsBigEndianKey设置音频解码是大字节序仍是小字节序,大字节序,YES,不然为NO [settings setValue:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey]; // AVLinearPCMIsFloatKey设置音频解码是否为浮点数,若是是则设置为YES,不然为NO [settings setValue:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey]; // 实例化recorder对象 recorder = [[AVAudioRecorder alloc] initWithURL:fileUrl settings:settings error:&error]; // 设置代理 recorder.delegate = self; } // 若是正在录制,return if(recorder.isRecording) { return; } //若是正在播放,中止 if(player && player.isPlaying) { [player stop]; } // 开始录制 [recorder record]; self.label.text = @"录制中..."; } - (IBAction)stop:(id)sender { self.label.text = @"中止..."; // 是否正在录制 if(recorder.isRecording) { [recorder stop];//中止 recorder.delegate = nil; recorder = nil; } if(player.isPlaying) { [player stop]; } } - (IBAction)play:(id)sender { if(recorder.isRecording) { [recorder stop]; recorder.delegate = nil; recorder = nil; } if(player.isPlaying) { [player stop]; } NSString *filePath = [NSString stringWithFormat:@"%@/rec_audio.caf", [self documentsDirectory]]; NSURL *fileUrl = [NSURL fileURLWithPath:filePath]; NSError *error = nil; [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&error]; [[AVAudioSession sharedInstance] setActive:YES error:&error]; player = [[AVAudioPlayer alloc] initWithContentsOfURL:fileUrl error:&error]; if(error) { NSLog(@"%@",[error description]); } else { [player play]; self.label.text = @"播放..."; } } -(NSString *)documentsDirectory{ NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); return [paths objectAtIndex:0]; } #pragma mark--实现AVAudioRecorderDelegate协议方法 - (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag{
NSLog(@"录制完成。"); } - (void)audioRecorderEncodeErrorDidOccur:(AVAudioRecorder *)recorder error:(NSError *)error{ NSLog(@"录制错误发生: %@", [error localizedDescription]); } - (void)audioRecorderBeginInterruption:(AVAudioRecorder *)recorder{ NSLog(@"播放中断。"); } - (void)audioRecorderEndInterruption:(AVAudioRecorder *)recorder withOptions:(NSUInteger)flags{ NSLog(@"中断返回。"); }