简体   繁体   English

Android Studio中如何配置MediaRecorder以48KHz的WAV格式录制音频?

[英]How to configure MediaRecorder to record audio in WAV format at 48KHz in Android Studio?

So far I have this: With this code I can configure Media Recorder to record in 3gp, but i need to record in WAV for a later process.到目前为止,我有这个:使用这段代码,我可以将 Media Recorder 配置为以 3gp 格式录制,但我需要以 WAV 格式录制以供稍后处理。

private void setupMediaRecorder() {
    mediaRecorder = new MediaRecorder();
    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    mediaRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
    mediaRecorder.setOutputFile(MainActivity.PATH_TEMP_RECORDING);
}

Found it. 找到了。 This configuration works: 此配置有效:

    mediaRecorder = new MediaRecorder();
    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mediaRecorder.setOutputFormat(AudioFormat.ENCODING_PCM_16BIT);
    mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
    mediaRecorder.setAudioChannels(1);
    mediaRecorder.setAudioEncodingBitRate(128000);
    mediaRecorder.setAudioSamplingRate(48000);
    mediaRecorder.setOutputFile(MainActivity.PATH_TEMP_RECORDING);

The MediaRecorder do not let you record in the lossless format. MediaRecorder 不允许您以无损格式进行录制。 Instead, AudioRecord is more low-level API for audio recording, while you need to write buffer code and filewriting code yourself.相反,AudioRecord 是更底层的 API 用于录音,而您需要自己编写缓冲区代码和文件写入代码。 Here is a example of using AudioRecord to write into.pcm file. 下面是一个使用 AudioRecord 写入.pcm 文件的例子。 WAV is basically a container of PCM 2 . WAV 基本上是 PCM 2的容器。 If you want to write into.wav, you need to write the header yourself, and here is an example of using AudioRecord to write into.wav.如果要写成.wav,需要自己写header,这里是用AudioRecord写成.wav的例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM