Android如何使用MediaRecorder录制音频并以原始PCM格式输出?

9

我目前使用MediaRecorder录制音频,生成.m4a、.mp4、.acc和.3gp格式的文件,并使用VisualizerView可视化每个录制的输出数据。

现在我想使用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();

}

你找到解决方案了吗? - anoop4real
1个回答

3
要获取原始的PCM数据,您需要使用AudioRecord类 - 它支持ENCODING_PCM_8BIT、ENCODING_PCM_16BIT和ENCODING_PCM_FLOAT。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接