AudioRecord api的认知

相比AudioTrack作用播音,AudioRecord的作用就是用来录音的。api也简单易懂,使用过程中根据自身需求获取相应录音通道的PCM音频数据。

1、用AudioRecord录制(read方法)下来直接保存在文件中的音频是PCM音频,也就是源音频,播放器没办法直接播放,需要转换为某一格式的音频。例如转为wave格式即可(在源文件前加44个字节来描述该音频------采样率、通道数、位宽等)。
2、AudioRecord没有暂停和继续录音的方法,只有start和stop。

adts头 ------ 7个字节

wave header ------44个字节

AudioRecord初始化
  • AudioRecord(int audioSource, int sampleRateInHz, int channelConfig, int audioFormat, int bufferSizeInBytes)
  1. audioSource:音频来源(mic);
  2. sampleRateInHz:采样率(16KHz 、44.1KHz)
  3. channelConfig:录音通道数
  4. audioFormat:位宽
  5. bufferSizeInBytes:缓冲区大小(AudioRecord.getMinBufferSize()可以拿到)
  • AudioRecord.getMinBufferSize(int sampleRateInHz, int channelConfig, int audioFormat):
  1. sampleRateInHz:采样率(16KHz 、44.1KHz)
  2. channelConfig:录音通道数
  3. audioFormat:位宽
AudioRecord方法

audioParamCheck(int audioSource, int sampleRateInHz,
int channelConfig, int audioFormat):
检查以下参数,无问题则设置。有问题抛出异常。

  1. mRecordSource is valid
  2. mChannelCount is valid
  3. mChannelMask is valid
  4. mAudioFormat is valid
  5. mSampleRate is valid
  • audioBuffSizeCheck(int audioBufferSize):
    检查缓冲区大小,无问题则设置。有问题抛出异常。
  • release():释放AudioRecord资源,release之后AudioRecord置为null。
  • int getSampleRate():返回采样率Hz
  • int getAudioSource():返回录音源信息
  • int getAudioFormat():位宽
  • int getChannelConfiguration():CHANNEL_IN_MONO(单声道) CHANNEL_IN_STEREO(立体声)
  • int getChannelCount()
  • int getState():实例是否初始化好。STATE_INITIALIZED(已准备好) STATE_UNINITIALIZED(未准备好)
  • int getRecordingState():是否在录音。RECORDSTATE_STOPPED(停止录音) RECORDSTATE_RECORDING(录音)
  • int getNotificationMarkerPosition():通知标志位
  • int getPositionNotificationPeriod():标志位更新时间
  • int getMinBufferSize(int sampleRateInHz, int channelConfig, int audioFormat):需要的最新缓冲区。
  • int getAudioSessionId():当前AudioRecord的属于的任务id
  • startRecording():开始录音
  • startRecording(MediaSyncEvent syncEvent):从一个特定的录音源录音
    stop():停止录音
  • int read(byte[] audioData, int offsetInBytes, int sizeInBytes):读取录音数据写入audioData从偏移量offsetInBytes到sizeInBytes
  • int read(short[] audioData, int offsetInShorts, int sizeInShorts):读取录音数据到audioData
  • int read(ByteBuffer audioBuffer, int sizeInBytes):读取录音数据到audioBuffer
  • setRecordPositionUpdateListener(OnRecordPositionUpdateListener listener)
  • setRecordPositionUpdateListener(OnRecordPositionUpdateListener listener,
    Handler handler):设置回调监听

接口定义为:当AudioRecord 收到一个由setNotificationMarkerPosition(int)设置的通知标志,或由 setPositionNotificationPeriod(int)设置的周期更新记录的进度状态时,回调此接口。

public interface OnRecordPositionUpdateListener  {
    /**
     * Called on the listener to notify it that the previously set marker has been reached
     * by the recording head.回调监听器,通知监听器已经到达之前设置的标记位置。
     */
    void onMarkerReached(AudioRecord recorder);

    /**
     * Called on the listener to periodically notify it that the record head has reached
     * a multiple of the notification period.按照一定的周期,通知监听器,指定的记录已经就绪。
     */
       void onPeriodicNotification(AudioRecord recorder);
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容