美文网首页
18期_AVFoundation_AVAudioRecorder

18期_AVFoundation_AVAudioRecorder

作者: 萧修 | 来源:发表于2023-09-26 01:35 被阅读0次

    简介

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

    设置录音参数

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

    • AVFormatIDKey:音频格式
      kAudioFormatLinearPCM
      kAudioFormatMPEG4AAC:音频格式能在显著减小文件的同时,保证音频的质量,同时安卓也支持acc格式音频

    • AVSampleRateKey:采样率
      AVSampleRateKey==8000/16000/44100/96000(影响音频的质量),采样率越高,文件越大,质量越高。反之,文件就越小,质量越低,但是低于普通的音频,人耳并不能分辨音频的好坏,最终选择那一种采样率,由我们耳朵来判断

    • 采样频率:是对声音1秒钟采样多少次,以记录数字信息,如CD音频是44.1KHZ采样率,它对声音以每秒44100次的频率来记录信息。原则上采样率越高,声音质量越好。

    • 比特率 - 表示单位时间内传送的比特数(bit per second,位/秒),作为一种数字音乐压缩效率的参考性指标,通常使用KBS(通俗地讲就是每秒钟1024比特)作为单位。

    • AVNumberOfChannelsKey:通道数目
      用于记录音频的通道数,1:单声道,2:立体声,除非使用外部硬件进行录制,否则通常会使用单声道录制。

    • AVLinearPCMBitDepthKey:采样位数
      可以理解为数字音频设备处理声音的解析度,即对声音的辨析度,就像表示颜色的位数一样,这个数值越大,解析度就越高,录制和回放的声音就越真实。
      8,16,32,64,默认是16位

    • AVEncoderAudioQualityKey:音频质量
      typedef NS_ENUM(NSInteger, AVAudioQuality) {
      AVAudioQualityMin = 0,
      AVAudioQualityLow = 0x20,
      AVAudioQualityMedium = 0x40,
      AVAudioQualityHigh = 0x60,
      AVAudioQualityMax = 0x7F
      };

    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];
    
    • AVAudioRecorderDelegate
    录制完成代理方法,被中断的代理方法AudioSession
    //
    - (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag{
        
    }
    

    相关文章

      网友评论

          本文标题:18期_AVFoundation_AVAudioRecorder

          本文链接:https://www.haomeiwen.com/subject/gedumdtx.html