Search code examples
androidaudiomicrophonerecordingmediarecorder

Android: Recording audio using MediaRecorder


I am trying to do a function in my app that records the sound recording then do some sound analysis after I got the sound recording.

I would like to use MediaRecorder library as I need the sound file and it seems to be a simpler option.

However, I can't seem to be able to start recording based on the http://developer.android.com/reference/android/media/MediaRecorder.html. EDIT: Thanks to @faz15, I can record audio without crashing. However, I cannot stop the recording, thus cannot get the sound clip. My code is below:

package helloworld.app;


import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;


import java.io.File;
import java.io.IOException;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;


public class StartRecording extends Activity {
    final AudioRecorder recorder = new AudioRecorder("/audiometer/temp"); 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.start_recording);
        //startRecording();
        try {
            recorder.start();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
    }
    public void stopRecording(View view) {
        try {
            recorder.stop();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Intent intent = new Intent(this, next_page.class);
        startActivity(intent);
    }
}

Solution

  • Have you added the appropriate permissions in your Android Manifest?