Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6069853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:52:04+00:00 2026-05-23T09:52:04+00:00

I taken sound recording code from http://developer.android.com/guide/topics/media/index.html , after running it on my emulator

  • 0

I taken sound recording code from
http://developer.android.com/guide/topics/media/index.html, after running it on my emulator it is throwing illegalStateException, I also tried in my device, but there also its same thing.

I am new to android, please help me

package com.android.audiorecordtest;

import android.app.Activity;
import android.widget.LinearLayout;
import android.os.Bundle;
import android.os.Environment;
import android.view.ViewGroup;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Context;
import android.util.Log;
import android.media.MediaRecorder;
import android.media.MediaPlayer;

import java.io.IOException;


public class AudioRecordTest extends Activity
{
    private static final String LOG_TAG = "AudioRecordTest";
    private static String mFileName = null;

    private RecordButton mRecordButton = null;
    private MediaRecorder mRecorder = null;

    private PlayButton   mPlayButton = null;
    private MediaPlayer   mPlayer = null;

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

    class RecordButton extends Button {
        boolean mStartRecording = true;

        OnClickListener clicker = new OnClickListener() {
            public void onClick(View v) {
                onRecord(mStartRecording);
                if (mStartRecording) {
                    setText("Stop recording");
                } else {
                    setText("Start recording");
                }
                mStartRecording = !mStartRecording;
            }
        };

        public RecordButton(Context ctx) {
            super(ctx);
            setText("Start recording");
            setOnClickListener(clicker);
        }
    }

    class PlayButton extends Button {
        boolean mStartPlaying = true;

        OnClickListener clicker = new OnClickListener() {
            public void onClick(View v) {
                onPlay(mStartPlaying);
                if (mStartPlaying) {
                    setText("Stop playing");
                } else {
                    setText("Start playing");
                }
                mStartPlaying = !mStartPlaying;
            }
        };

        public PlayButton(Context ctx) {
            super(ctx);
            setText("Start playing");
            setOnClickListener(clicker);
        }
    }

    public AudioRecordTest() {
        mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
        mFileName += "/audiorecordtest.3gp";
    }

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        LinearLayout ll = new LinearLayout(this);
        mRecordButton = new RecordButton(this);
        ll.addView(mRecordButton,
            new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                0));
        mPlayButton = new PlayButton(this);
        ll.addView(mPlayButton,
            new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                0));
        setContentView(ll);
    }

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

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

Stack Trace:

> 06-01 11:06:18.440:
> ERROR/AndroidRuntime(724): Uncaught
> handler: thread main exiting due to
> uncaught exception 06-01 11:06:18.449:
> ERROR/AndroidRuntime(724):
> java.lang.IllegalStateException 06-01
> 11:06:18.449:
> ERROR/AndroidRuntime(724):     at
> android.media.MediaRecorder.start(Native
> Method) 06-01 11:06:18.449:
> ERROR/AndroidRuntime(724):     at
> com.example.android.whereareyou.Main.startRecording(Main.java:75)
> 06-01 11:06:18.449:
> ERROR/AndroidRuntime(724):     at
> com.example.android.whereareyou.Main.onRecord(Main.java:32)
> 06-01 11:06:18.449:
> ERROR/AndroidRuntime(724):     at
> com.example.android.whereareyou.Main.access$0(Main.java:30)
> 06-01 11:06:18.449:
> ERROR/AndroidRuntime(724):     at
> com.example.android.whereareyou.Main$RecordButton$1.onClick(Main.java:89)
> 06-01 11:06:18.449:
> ERROR/AndroidRuntime(724):     at
> android.view.View.performClick(View.java:2179)
> 06-01 11:06:18.449:
> ERROR/AndroidRuntime(724):     at
> android.view.View.onTouchEvent(View.java:3828)
> 06-01 11:06:18.449:
> ERROR/AndroidRuntime(724):     at
> android.widget.TextView.onTouchEvent(TextView.java:6291)
> 06-01 11:06:18.449:
> ERROR/AndroidRuntime(724):     at
> android.view.View.dispatchTouchEvent(View.java:3368)
> 06-01 11:06:18.449:
> ERROR/AndroidRuntime(724):     at
> android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
> 06-01 11:06:18.449:
> ERROR/AndroidRuntime(724):     at
> android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
> 06-01 11:06:18.449:
> ERROR/AndroidRuntime(724):     at
> android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
> 06-01 11:06:18.449:
> ERROR/AndroidRuntime(724):     at
> android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
> 06-01 11:06:18.449:
> ERROR/AndroidRuntime(724):     at
> com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1707)
> 06-01 11:06:18.449:
> ERROR/AndroidRuntime(724):     at
> com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1197)
> 06-01 11:06:18.449:
> ERROR/AndroidRuntime(724):     at
> android.app.Activity.dispatchTouchEvent(Activity.java:1993)
> 06-01 11:06:18.449:
> ERROR/AndroidRuntime(724):     at
> com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1691)
> 06-01 11:06:18.449:
> ERROR/AndroidRuntime(724):     at
> android.view.ViewRoot.handleMessage(ViewRoot.java:1525)
> 06-01 11:06:18.449:
> ERROR/AndroidRuntime(724):     at
> android.os.Handler.dispatchMessage(Handler.java:99)
> 06-01 11:06:18.449:
> ERROR/AndroidRuntime(724):     at
> android.os.Looper.loop(Looper.java:123)
> 06-01 11:06:18.449:
> ERROR/AndroidRuntime(724):     at
> android.app.ActivityThread.main(ActivityThread.java:3948)
> 06-01 11:06:18.449:
> ERROR/AndroidRuntime(724):     at
> java.lang.reflect.Method.invokeNative(Native
> Method) 06-01 11:06:18.449:
> ERROR/AndroidRuntime(724):     at
> java.lang.reflect.Method.invoke(Method.java:521)
> 06-01 11:06:18.449:
> ERROR/AndroidRuntime(724):     at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
> 06-01 11:06:18.449:
> ERROR/AndroidRuntime(724):     at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
> 06-01 11:06:18.449:
> ERROR/AndroidRuntime(724):     at
> dalvik.system.NativeStart.main(Native
> Method)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:52:05+00:00Added an answer on May 23, 2026 at 9:52 am

    Try to use that:-

    public class VoiceRecorder extends Activity {
    /** Called when the activity is first created. */
    private MediaRecorder mediaRecorder;
    boolean recording= false;
    private Button start; //Start Button to start the recordings
    private Button stop; // Stop button to stop the recordings
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.voice_recorder);
        mediaRecorder = new MediaRecorder();
        start = (Button)findViewById(R.id.start);
    
        //Activity when user click the start Button
        start.setOnClickListener(new View.OnClickListener() {
    
            public void onClick(View v) {
                // TODO Auto-generated method stub
                settings();
                recordPrepare();
                mediaRecorder.start();
                recording = true;
    
            }
    
        });
    
        //Activity when user click the stop Button
        stop = (Button)findViewById(R.id.stop);
        stop.setOnClickListener(new View.OnClickListener() {
    
            public void onClick(View v) {
                // TODO Auto-generated method stub
    
                if(recording)
                {
                    mediaRecorder.stop();
                    mediaRecorder.release();
                    recording = false;
                    settings();
                    recordPrepare();
                }
    
            }
        });
    }
    //Settings for Audio Recorder
    private void settings()
    {
        long name = new Date().getTime();
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
        mediaRecorder.setOutputFile("/sdcard/"+Long.toString(name)+".3gp");
        DataModel dbModel = new DataModel(this);
        Calendar cal = Calendar.getInstance();
        dbModel.insertMultimedia("/sdcard/"+Long.toString(name)+".3gp", Long.toString(name), cal);
    }
    //prepare the Environment for Recording
    private void recordPrepare()
    {
        try
        {
            mediaRecorder.prepare();
        }
        catch(IllegalStateException e)
        {
            e.printStackTrace();
    
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }
    }
    

    }

    This will work fine. and file will be stored into SD card. you can also play by using play method as you have used in your code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've taken over a mixed PHP4/PHP5 project which has been handed down from developer
Example taken from Mozilla's help page <script type="text/javascript"> re = /(\w+)\s(\w+)/; str = "John
I have taken over a large code base and would like to get an
I have taken an image from UIImagePickerController , but the file looks too big
I've taken the plunge and upgraded (or maybe downgraded?!) from WinXP to Vista. Everything
I ran into this supposed interview of Bjarne Stroustrup, the inventor of C++. http://artlung.com/smorgasborg/Invention_of_Cplusplus.shtml
I am trying to take small recordings to find the Sound Pressure Level from
this may sound like a strange question from a Python noob, but here's the
I want a link to trigger a sound click in flash AS3. I've taken
This question title is taken from the title of item #23 in Effective C++

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.