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

  • SEARCH
  • Home
  • 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 8076563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:17:06+00:00 2026-06-05T15:17:06+00:00

I am trying to record audio by MediaRecorder, and I get an error, I

  • 0

I am trying to record audio by MediaRecorder, and I get an error, I tried to change everything and nothing works. Last two hours I try to find the error, I used Log class too and I found out that error occurred when it call recorder.start() method. What could be the problem?

public class AudioRecorderActivity extends Activity {

MediaRecorder recorder;
File audioFile = null;
private static final String TAG = "AudioRecorderActivity";
private View startButton;
private View stopButton;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    startButton = findViewById(R.id.start);
    stopButton = findViewById(R.id.stop);

    setContentView(R.layout.main);
}

public void startRecording(View view) throws IOException{

    startButton.setEnabled(false);
    stopButton.setEnabled(true);

    File sampleDir = Environment.getExternalStorageDirectory();

    try{
        audioFile = File.createTempFile("sound", ".3gp", sampleDir);
    }catch(IOException e){
        Toast.makeText(getApplicationContext(), "SD Card Access Error", Toast.LENGTH_LONG).show();
        Log.e(TAG, "Sdcard access error");
        return;
    }

    recorder = new MediaRecorder();
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    recorder.setAudioEncodingBitRate(16);
    recorder.setAudioSamplingRate(44100);
    recorder.setOutputFile(audioFile.getAbsolutePath());
    recorder.prepare();
    recorder.start();
}

public void stopRecording(View view){
    startButton.setEnabled(true);
    stopButton.setEnabled(false);
    recorder.stop();
    recorder.release();
    addRecordingToMediaLibrary();
}

protected void addRecordingToMediaLibrary(){
    ContentValues values = new ContentValues(4);
    long current = System.currentTimeMillis();
    values.put(MediaStore.Audio.Media.TITLE, "audio" + audioFile.getName());
    values.put(MediaStore.Audio.Media.DATE_ADDED, (int)(current/1000));
    values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/3gpp");
    values.put(MediaStore.Audio.Media.DATA, audioFile.getAbsolutePath());
    ContentResolver contentResolver = getContentResolver();

    Uri base = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    Uri newUri = contentResolver.insert(base, values);

    sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, newUri));
    Toast.makeText(this, "Added File" + newUri, Toast.LENGTH_LONG).show();
    }

} 

And here is the xml layout.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<Button
    android:id="@+id/start"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="146dp"
    android:onClick="startRecording"
    android:text="Start Recording" />

<Button
    android:id="@+id/stop"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/start"
    android:layout_below="@+id/start"
    android:layout_marginTop="41dp"
    android:enabled="false"
    android:onClick="stopRecording"
    android:text="Stop Recording" />

 </RelativeLayout>

And I added permission to AndroidManifest file.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="in.isuru.audiorecorder"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" />


<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".AudioRecorderActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

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

</manifest>

This is the error I get,

 06-11 18:27:44.548: E/AndroidRuntime(765): java.lang.IllegalStateException: Could not execute method of the activity
 06-11 18:27:44.548: E/AndroidRuntime(765):     at android.view.View$1.onClick(View.java:2072)

I need to record high quality audio.

Thanks!

  • 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-06-05T15:17:07+00:00Added an answer on June 5, 2026 at 3:17 pm

    I didn’t take the time to bug hunt your code, but this code works:

    public class ExampleActivity extends Activity implements OnClickListener {
    private Button startButton;
    private Button stopButton;
    private MediaRecorder mediaRecorder;
    private File audioFile;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.your_layout);
        startButton = (Button) findViewById(R.id.button1);
        startButton.setOnClickListener(this);
        startButton.setText("start");
    
        stopButton = (Button) findViewById(R.id.button2);
        stopButton.setOnClickListener(this);
        stopButton.setEnabled(false);
        stopButton.setText("stop");
    
        audioFile = new File(Environment.getExternalStorageDirectory(),
                "audio_test4.3gp");
    }
    
    // this process must be done prior to the start of recording
    private void resetRecorder() {
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        mediaRecorder.setAudioEncodingBitRate(16);
        mediaRecorder.setAudioSamplingRate(44100);
        mediaRecorder.setOutputFile(audioFile.getAbsolutePath());
    
        try {
            mediaRecorder.prepare();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.button1:
            mediaRecorder = new MediaRecorder();
            resetRecorder();
            mediaRecorder.start();
    
            startButton.setEnabled(false);
            stopButton.setEnabled(true);
            break;
        case R.id.button2:
            mediaRecorder.stop();
            mediaRecorder.release();
            mediaRecorder = null;
    
            startButton.setEnabled(true);
            stopButton.setEnabled(false);
            break;
        }
    }
    
    @Override
    protected void onPause() {
        super.onPause();
    
        if (mediaRecorder != null) {
            mediaRecorder.stop();
            mediaRecorder.release();
            mediaRecorder = null;
        }
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to record audio this.recorder = new android.media.MediaRecorder(); this.recorder.setAudioSource(android.media.MediaRecorder.AudioSource.MIC); this.recorder.setOutputFormat(android.media.MediaRecorder.OutputFormat.DEFAULT); this.recorder.setAudioEncoder(android.media.MediaRecorder.AudioEncoder.DEFAULT); this.recorder.setOutputFile(pruebaAudioRecorder.mp4); **this.recorder.prepare();**
Simply stated: I'm trying to record audio in a browser, and get that data
I've tried to overcome this for a while. I'm trying to record sound, but
I am new to Nhibernate, I am trying to get record by ID and
I'm trying to use android.media.MediaRecorder to record video, and no matter what I do
i've been trying to record and play recorded audio in android . I've used
I'm trying to get started with a simple audio application under .NET 3.5 (preferably
Hello There, I am new to phonegap.I am trying to record a audio clip
I'm trying to record a sample audio with phoneGap, but with no success. What
i'm trying to record an audio sample with AudioRecord. I've used a thread to

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.