【问题标题】:Android How To record audio using MediaRecorder and output as raw PCM?Android 如何使用 MediaRecorder 录制音频并输出为原始 PCM?
【发布时间】:2017-06-14 22:13:00
【问题描述】:

我目前使用 MediaRecorder 录制音频以生成 .m4a、.mp4、.acc 和 .3gp,还可视化每个记录的输出数据 可视化视图。

现在我想用 PCM 做同样的事情。

我尝试使用带有 ENCODING_PCM_16BIT 输入的 MediaRecorder 和几种不同的输出组合。但这些组合都不会产生原始 PCM 输出。

我的问题是:如何使用 MediaRecorder 进行录制并将结果输出为原始 PCM?

Java 代码:

public String mFileName = null;
private String fileName = null;
private String fileNamePcm = null;

String formattedDate = new SimpleDateFormat("MM-dd-yyyy_HH-mm-ss").format(new Date());
fileName = formattedDate;
fileNamePcm = formattedDate;

MediaRecorder mRecorder = new MediaRecorder();

startRecording() {

    // Test Format 1 - produced AMR_WB file
    mRecorder.setAudioSamplingRate(44100);
    mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mRecorder.setOutputFormat(AudioFormat.CHANNEL_OUT_MONO); // produced AMR_WB file.
    mRecorder.setAudioEncoder(AudioFormat.ENCODING_PCM_16BIT);


    // Test Format 2 - produced 3gp file
    mRecorder.setAudioSamplingRate(44100);
    mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); // produced 3gp file.
    mRecorder.setAudioEncoder(AudioFormat.ENCODING_PCM_16BIT);


    // Test Format 3 - produced 3gp file
    mRecorder.setAudioSamplingRate(44100);
    mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); // did not produced a file.
    mRecorder.setAudioEncoder(AudioFormat.ENCODING_PCM_16BIT);
    mRecorder.setOutputFile(fileNamePcm);  // produced 3gp file. // trying to write raw pcm output to file.


    mFileName = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + "TestRecordings" + "/" + fileName + ".3gp";
    mFileName = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + "TestRecordings" + "/" + fileNamePcm + ".pcm";

    mRecorder.setOutputFile(mFileName);

    try {
        mRecorder.prepare();
    } catch (IOException e) {
        Log.e(LOG_TAG, "prepare() failed");
    } catch (IllegalStateException e) {
        e.printStackTrace();
    }

    mRecorder.start();

}

【问题讨论】:

  • 你找到解决办法了吗?

标签: java android audio-recording pcm android-mediarecorder


【解决方案1】:

要获取原始 PCM 数据,您必须使用 AudioRecord class - 它支持 ENCODING_PCM_8BIT、ENCODING_PCM_16BIT 和 ENCODING_PCM_FLOAT。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-16
    • 1970-01-01
    • 1970-01-01
    • 2021-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多