title: Record audio via MediaRecorder
body: <p>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?</p>
  
  <pre><code>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();
      }
  
  } 
  </code></pre>
  
  <p>And here is the xml layout.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;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" &gt;
  
  &lt;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" /&gt;
  
  &lt;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" /&gt;
  
   &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>And I added permission to AndroidManifest file.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="in.isuru.audiorecorder"
  android:versionCode="1"
  android:versionName="1.0" &gt;
  
  &lt;uses-sdk android:minSdkVersion="8" /&gt;
  
  
  &lt;application
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name" &gt;
      &lt;activity
          android:name=".AudioRecorderActivity"
          android:label="@string/app_name" &gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.MAIN" /&gt;
  
              &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
          &lt;/intent-filter&gt;
      &lt;/activity&gt;
  &lt;/application&gt;
  
  &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/&gt;
  &lt;uses-permission android:name="android.permission.RECORD_AUDIO" /&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p>This is the error I get,</p>
  
  <pre><code> 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)
  </code></pre>
  
  <p>I need to record high quality audio.</p>
  
  <p>Thanks!</p>