18期_AVFoundation_AVAudioRecorder

2023-09-26  本文已影响0人  萧修

简介

使用AVFAudio库下AVAudioRecorder,实现音频录制,用于从内置麦克风或者其他音频输入设备录制音频。

设置录音参数

包含一些音频格式,采样率,声道等配置信息,这些信息用字典统计处理

AVAudioRecorder类

1、启动恢复录制,取消,暂停,停止,删除录制文件

/* methods that return BOOL return YES on success and NO on failure. */
- (BOOL)prepareToRecord; /* creates the file and gets ready to record. happens automatically on record. */
- (BOOL)record; /* start or resume recording to file. */
- (BOOL)recordAtTime:(NSTimeInterval)time API_AVAILABLE(macos(10.9), ios(6.0), watchos(4.0)) API_UNAVAILABLE(tvos); /* start recording at specified time in the future. time is an absolute time based on and greater than deviceCurrentTime. */
- (BOOL)recordForDuration:(NSTimeInterval) duration; /* record a file of a specified duration. the recorder will stop when it has recorded this length of audio */
- (BOOL)recordAtTime:(NSTimeInterval)time forDuration:(NSTimeInterval) duration API_AVAILABLE(macos(10.9), ios(6.0), watchos(4.0)) API_UNAVAILABLE(tvos); /* record a file of a specified duration starting at specified time. time is an absolute time based on and greater than deviceCurrentTime. */
- (void)pause; /* pause recording */
- (void)stop; /* stops recording. closes the file. */

- (BOOL)deleteRecording; /* delete the recorded file. recorder must be stopped. returns NO on failure. */

开启音量检测

//开启音量检测
audioRecorder.meteringEnabled = YES;

业务场景绘制波形,示例代码

    //更新音量数据
    [self.recorder updateMeters];
    //获取音量最大值,声音分贝峰值大小,但是取值范围是-160~0,声音峰值越大,越接近0。
    [self.recorder peakPowerForChannel:0];
    //pow返回一个数,乘方所得的幂,比如power(10,2)为10的2次方
    double lowPassResults = pow(10, (0.1 * [self.recorder peakPowerForChannel:0]));
    
    //获取音量平均值
    [self.recorder averagePowerForChannel:0];
录制完成代理方法,被中断的代理方法AudioSession
//
- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag{
    
}
上一篇 下一篇

猜你喜欢

热点阅读