Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Record audio in AMR file format

I wan to record audio in AMR file format. I am currently using bellow code to record audio:

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);

But it generates .3gp file. How can I get .amr file? Changing outputfile to Sample.amr works. But is it a correct way? Please help

EDIT ITS SOLVED NOW

It was my Silly Mistake: I used myRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);

It should be-

myRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

So bellow code is working for Recording in AMR format:

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);
like image 333
AnujDeo Avatar asked Oct 31 '14 13:10

AnujDeo


1 Answers

Refer to android OutputFormat document Try below code:

    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");

Key points:

  1. Out file name use ".amr" suffix. Output format use OutputFormat.AMR_NB parameter.
  2. Encorder use AudioEncoder.AMR_NB parameter.
like image 104
lucky1928 Avatar answered Sep 28 '22 01:09

lucky1928