0
votes

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);
    }
}
3
Shouldn't you also specify a file name for the output file rather than just a directory path? Also, on most devices you won't have write permissions to "/". - Michael
Do you happened to know where can I get the sound clips that was recorded on the emulator? - Sakura

3 Answers

1
votes

Have you added the appropriate permissions in your Android Manifest?

0
votes

Just in case , add the required permissions for it in the

android.permission.RECORD_AUDIO

Maybe your virtual device needs to have audio recording & playback support? Edit your AVD, and add that to the hardware.

0
votes

I'm not sure what you mean by "you cannot stop recording".

However, if it helps anybody, recorder.release() should be called after stop() and recorder must be set to null if you're done with the object.