【问题标题】:Record audio in AMR file format以 AMR 文件格式录制音频
【发布时间】:2014-10-31 13:34:40
【问题描述】:

我想以 AMR 文件格式录制音频。我目前正在使用下面的代码来录制音频:

outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "Sample.3gp";

myRecorder = new MediaRecorder();
myRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
myRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
myRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
myRecorder.setOutputFile(outputFile);

但它会生成 .3gp 文件。如何获取 .amr 文件? 将输出文件更改为 Sample.amr 有效。但这是正确的方法吗?请帮助

编辑 现在解决了

这是我的愚蠢错误:我使用了myRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);

应该是-

myRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

所以下面的代码适用于以 AMR 格式录制:

outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "Sample.amr";

    myRecorder = new MediaRecorder();
    myRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    myRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
    myRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    myRecorder.setOutputFile(outputFile);

【问题讨论】:

    标签: android audio-recording mediarecorder


    【解决方案1】:

    参考android OutputFormat document 试试下面的代码:

        Log.i(TAG,"Record start");
        String outputFile;
        MediaRecorder myRecorder;
        outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + "Sample.amr";
        Log.i(TAG,"file name: " + outputFile);
    
        myRecorder = new MediaRecorder();
        myRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        myRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
        myRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        myRecorder.setOutputFile(outputFile);
    
        try {
            myRecorder.prepare();
            myRecorder.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            Thread.sleep(30*1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        myRecorder.stop();
        myRecorder.release();
        Log.i(TAG,"Record finished");
    

    关键点:

    1. 输出文件名使用“.amr”后缀。输出格式使用 OutputFormat.AMR_NB 参数。
    2. 编码器使用 AudioEncoder.AMR_NB 参数。

    【讨论】:

    • 谢谢。但我已经尝试过了。当我们开始录制错误时,它会强制关闭应用程序:Start failed -2​​2 java.lang.RuntimeException: Start Failed
    • 你有没有先调用prepare,然后调用start。如果您可以共享整个文件,将会很有帮助。
    • 检查我的编辑版本,它适用于 moto g 设备。你的设备是什么?可能是设备问题。
    • 是的,我先调用准备然后开始。我已经扎根三星 Galaxy Ace(OS Cyanogen mod 7 - 2.3.7)。是因为它的根吗?我将在其他设备上进行测试并回复您。谢谢。
    • 嘿@Beetlej 谢谢!它现在正在工作。这是我的愚蠢错误。我用myRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
    猜你喜欢
    • 1970-01-01
    • 2010-09-21
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 2011-06-19
    • 1970-01-01
    • 2014-02-13
    相关资源
    最近更新 更多