使用框架AVFoundation中的AVAudioRecorder类,属性与方法大体和AVAudioPlayer类似,但是在创建录音机时除了指定路径外还必须指定录音设置信息,
因为录音机必须知道录音文件的格式、采样率、通道数、每个采样点的位数等信息,通常需要设置几个常用属性。
AVAudioRecorder类对象 属性说明
@property(readonly, getter=isRecording) BOOL recording; 是否正在录音,只读
@property(readonly) NSURL *url 录音文件地址,只读
@property(readonly) NSDictionary *settings 录音文件设置,只读
@property(readonly) NSTimeInterval currentTime 录音时长,只读,注意仅仅在录音状态可用
@property(readonly) NSTimeInterval deviceCurrentTime 输入设置的时间长度,只读,注意此属性一直可访问
@property(getter=isMeteringEnabled) BOOL meteringEnabled; 是否启用录音测量,如果启用录音测量可以获得录音分贝等数据信息
@property(nonatomic, copy) NSArray *channelAssignments 当前录音的通道
AVAudioRecorder类对象 对象方法说明
- (instancetype)initWithURL:(NSURL *)url settings:(NSDictionary *)settings error:(NSError **)outError 录音机对象初始化方法,注意其中的url必须是本地文件 url,settings是录音设置
- (BOOL)prepareToRecord 准备录音,主要用于创建缓冲区,如果不手动调用,在调用record录音时也会自动调用
- (BOOL)record 开始录音
- (BOOL)recordAtTime:(NSTimeInterval)time 在指定的时间开始录音,一般用于录音暂停再恢复录音
- (BOOL)recordForDuration:(NSTimeInterval) duration 按指定的时长开始录音
- (BOOL)recordAtTime:(NSTimeInterval)time forDuration:(NSTimeInterval) duration 在指定的时间开始录音,并指定录音时长
- (void)pause; 暂停录音
- (void)stop; 停止录音
- (BOOL)deleteRecording; 删除录音,注意要删除录音此时录音机必须处于停止状态
- (void)updateMeters; 更新测量数据,注意只有meteringEnabled为YES此方法才可用
- (float)peakPowerForChannel:(NSUInteger)channelNumber; 指定通道的测量峰值,注意只有调用完updateMeters才有值
- (float)averagePowerForChannel:(NSUInteger)channelNumber 指定通道的测量平均值,注意只有调用完updateMeters才有值
AVAudioRecorder类对象 代理方法说明
- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag 完成录音
- (void)audioRecorderEncodeErrorDidOccur:(AVAudioRecorder *)recorder error:(NSError *)error 录音编码发生错误
简单书写步骤
1.导入框架AVFoundation,引入头文件#import <AVFoundation/AVFoundation.h>
2.首先设置录音文件保存路径
NSString *urlStr=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
urlStr=[urlStr stringByAppendingPathComponent:文件全名(文件名+类型)];
NSURL *strFinalUrl=[NSURL fileURLWithPath:urlStr];
3.设置录音文件设置信息(以下是部分信息)
NSMutableDictionary *dicSetting=[NSMutableDictionary dictionary];
[dicSetting setObject:@(kAudioFormatLinearPCM) forKey:AVFormatIDKey]; //设置录音格式
[dicSetting setObject:@(8000) forKey:AVSampleRateKey];//设置录音采样率,8000是电话采样率,对于一般录音已经够了
[dicSetting setObject:@(1) forKey:AVNumberOfChannelsKey]; //设置通道,这里采用单声道
[dicSetting setObject:@(8) forKey:AVLinearPCMBitDepthKey]; //每个采样点位数,分为8、16、24、32
[dicSetting setObject:@(YES) forKey:AVLinearPCMIsFloatKey];//是否使用浮点数采样
4.创建录音对象,准备录音
AVAudioRecorder*audioRecorder=[[AVAudioRecorder alloc]initWithURL:strFinalUrl settings:dicSetting error:&error];
audioRecorder.delegate=self;
audioRecorder.meteringEnabled=YES;//如果要监控声波则必须设置为YES
5.一些录音期间的基本操作,开始、暂停、停止
[audioRecorder record];//开始
[audioRecorder pause];//暂停
[audioRecorder stop];//停止
6.录音回调
-(void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag{
NSLog(@"录音完成!");
录音需要播放
AVAudioPlayer*audioPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:strFinalUrl error:&error];
audioPlayer.numberOfLoops=0;
[audioPlayer prepareToPlay];
[audioPlayer play];
}