Question

I'm trying to make a simple application that will record an audio file and store it in local storage but for some reason my code isn't working! I'm wondering if there is a problem with my code! Please help to find the mistake!

public class demo extends Activity implements OnClickListener {

    private static final String LOG_TAG = "AudioRecordTest";

    private static String mFileName = null;

    private MediaRecorder mRecorder = null;

    private MediaPlayer mPlayer = null;

    boolean mStartPlaying, mStartRecording;

    Button RecordButton, PlayButton;

    private void onRecord(boolean start) {
        if (start) {
            startRecording();
        } else {
            stopRecording();
        }
    }

    private void onPlay(boolean start) {
        if (start) {
            startPlaying();
        } else {
            stopPlaying();
        }
    }

    private void startPlaying() {
        mPlayer = new MediaPlayer();
        try {
            mPlayer.setDataSource(mFileName);
            mPlayer.prepare();
            mPlayer.start();
        } catch (IOException e) {
            Log.e(LOG_TAG, "prepare() failed");
        }
    }

    private void stopPlaying() {
        mPlayer.release();
        mPlayer = null;
    }

    private void startRecording() {
        mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mRecorder.setOutputFile(mFileName);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

        try {
            mRecorder.prepare();
        } catch (IOException e) {
            Log.e(LOG_TAG, "prepare() failed");
        }

        mRecorder.start();
    }

    private void stopRecording() {
        mRecorder.stop();
        mRecorder.release();
        mRecorder = null;
    }

    public demo() {
        mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
        mFileName += "my_Record";
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);

        mStartRecording = true;
        mStartPlaying = true;

        RecordButton = (Button) findViewById(R.id.btRecord);
        RecordButton.setOnClickListener(this);
        PlayButton = (Button) findViewById(R.id.btPlay);
        PlayButton.setOnClickListener(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mRecorder != null) {
            mRecorder.release();
            mRecorder = null;
        }

        if (mPlayer != null) {
            mPlayer.release();
            mPlayer = null;
        }
    }

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub

        switch (arg0.getId()) {

        case R.id.btRecord: {

            onRecord(mStartRecording);
            if (mStartRecording) {
                RecordButton.setText("Stop recording");
            } else {
                RecordButton.setText("Start recording");
            }
            mStartRecording = !mStartRecording;
            break;
        }
        case R.id.btPlay: {

            onPlay(mStartPlaying);
            if (mStartPlaying) {
                PlayButton.setText("Stop playing");
            } else {
                PlayButton.setText("Start playing");
            }
            mStartPlaying = !mStartPlaying;
            break;
        }
        }
    }
}

LogCat output:

11-28 13:57:01.562: E/AndroidRuntime(2237): FATAL EXCEPTION: main
11-28 13:57:01.562: E/AndroidRuntime(2237): Process: com.example.drishtikon, PID: 2237
11-28 13:57:01.562: E/AndroidRuntime(2237): java.lang.IllegalStateException
11-28 13:57:01.562: E/AndroidRuntime(2237):     at android.media.MediaRecorder.start(Native Method)
11-28 13:57:01.562: E/AndroidRuntime(2237):     at com.example.drishtikon.demo.startRecording(demo.java:78)
11-28 13:57:01.562: E/AndroidRuntime(2237):     at com.example.drishtikon.demo.onRecord(demo.java:35)
11-28 13:57:01.562: E/AndroidRuntime(2237):     at com.example.drishtikon.demo.onClick(demo.java:129)
11-28 13:57:01.562: E/AndroidRuntime(2237):     at android.view.View.performClick(View.java:4424)
11-28 13:57:01.562: E/AndroidRuntime(2237):     at android.view.View$PerformClick.run(View.java:18383)
11-28 13:57:01.562: E/AndroidRuntime(2237):     at android.os.Handler.handleCallback(Handler.java:733)
11-28 13:57:01.562: E/AndroidRuntime(2237):     at android.os.Handler.dispatchMessage(Handler.java:95)
11-28 13:57:01.562: E/AndroidRuntime(2237):     at android.os.Looper.loop(Looper.java:137)
11-28 13:57:01.562: E/AndroidRuntime(2237):     at android.app.ActivityThread.main(ActivityThread.java:4998)
11-28 13:57:01.562: E/AndroidRuntime(2237):     at java.lang.reflect.Method.invokeNative(Native Method)
11-28 13:57:01.562: E/AndroidRuntime(2237):     at java.lang.reflect.Method.invoke(Method.java:515)
11-28 13:57:01.562: E/AndroidRuntime(2237):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
11-28 13:57:01.562: E/AndroidRuntime(2237):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
11-28 13:57:01.562: E/AndroidRuntime(2237):     at dalvik.system.NativeStart.main(Native Method)
Was it helpful?

Solution

It's the filepath (mFileName);

Correct your demo() method. This is how it should look like:

public demo() {
        mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
        mFileName += "/my_Record";
    }

You need to add a slash (/) between the directory path and the filename, of course. Also try adding a file extension, like "/my_Record.3gp" if you use the OutputFormat.THREE_GPP.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top