Search code examples
androidaudiocreatefilerecorder

How to create a .wav file after a record - Android Studio


I try to make a simple recorder and when I start the operation i get fileNotFoundException. How do i create a .wav file in a specific folder, let's say on the external card, so i can access that folder later for listening the records stored there.

This is my code so far , i used a library the record activity :

    public class MainActivity extends Activity {
    String filePath;
    int color ;
    int requestCode = 0;
    Button recordButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recordButton = (Button) findViewById(R.id.recordButton);

        recordButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                filePath = Environment.getExternalStorageDirectory() + "/recorded_audio.wav";
                color = getResources().getColor(R.color.colorPrimaryDark);
                AndroidAudioRecorder.with(MainActivity.this)
                        .setFilePath(filePath)
                        .setColor(color)
                        .setRequestCode(requestCode)
                        .record();
            }
        });


    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0) {
            if (resultCode == RESULT_OK) {
                Toast.makeText(MainActivity.this, "Resoult ok", Toast.LENGTH_SHORT).show();
                // Great! User has recorded and saved the audio file
            } else if (resultCode == RESULT_CANCELED) {
                Toast.makeText(MainActivity.this, "Resoult not-ok", Toast.LENGTH_SHORT).show();
                // Oops! User has canceled the recording
            }
        }
    }
}

Solution

  • Android has a built in microphone through which you can capture audio and store it , or play it in your phone. There are many ways to do that but the most common way is through MediaRecorder class.

    Android provides MediaRecorder class to record audio or video. In order to use MediaRecorder class ,you will first create an instance of MediaRecorder class. Its syntax is given below.

    MediaRecorder myAudioRecorder = new MediaRecorder();
    

    Now you will set the source , output and encoding format and output file. Their syntax is given below.

    myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
    myAudioRecorder.setOutputFile(outputFile);
    

    After specifying the audio source and format and its output file, we can then call the two basic methods prepare and start to start recording the audio.

    myAudioRecorder.prepare();
    myAudioRecorder.start();
    

    In your code just do following changes :-

     outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/recording.3gp";;
    
          myAudioRecorder=new MediaRecorder();
          myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
          myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
          myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
          myAudioRecorder.setOutputFile(outputFile);
    
          record.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                try {
                   myAudioRecorder.prepare();
                   myAudioRecorder.start();
                }
    
                catch (IllegalStateException e) {
                   // TODO Auto-generated catch block
                   e.printStackTrace();
                }
    
                catch (IOException e) {
                   // TODO Auto-generated catch block
                   e.printStackTrace();
                }
    
                record.setEnabled(false);
                stop.setEnabled(true);
    
                Toast.makeText(getApplicationContext(), "Recording started", Toast.LENGTH_LONG).show();
             }
          });
    

    Hope it helps and put permissions also in your android manifest file .

     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
       <uses-permission android:name="android.permission.RECORD_AUDIO" />