title: MediaRecorder crash
body: <hr>
  
  <p>Very beginner in Android/Java, coming from C and Symbian, I started from the Hellworld example to try to display the sound level (the project is to record this level in a text file for hours).
  This code is written in the MainActivity file, as a method inside the MainActivity Class :</p>
  
  <pre><code>public void GetAFlevel(View view) throws IllegalStateException, IOException {
      float audiolevel = 0, maxal = 0;
      MediaRecorder mRecorder = new MediaRecorder();
      mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
      mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
      mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
      mRecorder.setOutputFile("/dev/null");
      mRecorder.prepare();
      mRecorder.start();
      for (int i = 0; i &lt; 1000; i++) {
          audiolevel = mRecorder.getMaxAmplitude();
          if (audiolevel &gt; maxal)
              maxal = audiolevel;
      }
      EditText editText1 = (EditText) findViewById(R.id.editText1);
      editText1.setText(String.valueOf(maxal));
      mRecorder.stop();
      mRecorder.release();
      mRecorder = null;
  }
  </code></pre>
  
  <p>The result is a crash of the application on the target :</p>
  
  <pre><code>11-17 00:23:09.859: D/AndroidRuntime(22096): Shutting down VM
  
  11-17 00:23:09.859: W/dalvikvm(22096): threadid=1: thread exiting with uncaught exception (group=0x40018578)
  
  11-17 00:23:09.921: E/AndroidRuntime(22096): FATAL EXCEPTION: main
  
  11-17 00:23:09.921: E/AndroidRuntime(22096): java.lang.IllegalStateException: Could not execute method of the activity
  </code></pre>
  
  <p>No crash if I remove the following MediaRecorder functions : setAudioSource, setOutputFormat, setAudioEncoder, setOutputFile, prepare, start and stop, but getMaxAmplitude  returns 0, of course.</p>
  
  <p>As soon as I add only setAudioSource (normaly the only necessary for getMaxAmplitude), crash !</p>
  
  <p>Any idea ?</p>
  
  <p>Thanks in advance.</p>
  
  <hr>
  
  <p>Hi</p>
  
  <p>Finally found a way to have a working code : I put it inside the OnCreate Method :</p>
  
  <pre><code>package com.example.mfa;
  
  import java.io.IOException;
  import android.os.Bundle;
  import android.app.Activity;
  import android.widget.EditText;
  import android.media.MediaRecorder;
  
  public class MainActivity extends Activity {
      public MediaRecorder mRecorder  =null;
      public float audiolevel=0,maxal=0;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      MediaRecorder mRecorder  = new MediaRecorder();
      mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
      mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
      mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
      mRecorder.setOutputFile("/dev/null");
      try {
          mRecorder.prepare();
      } catch (IllegalStateException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      }
      mRecorder.start();
          for (int i=0;i&lt;10000;i++){
          audiolevel=mRecorder.getMaxAmplitude ();
          if (audiolevel&gt;maxal) maxal=audiolevel;}
          EditText editText1 = (EditText) findViewById(R.id.editText1);
          editText1.setText(String.valueOf(maxal));   
      mRecorder.stop();
      mRecorder.release();
      mRecorder = null;
      }
  }
  </code></pre>
  
  <p>remarks : no problem with setOutputFile("/dev/null");<br>
  mRecorder.prepare(); surrounded with try/catch</p>
  
  <p>OK but if I put the same code inside an other method called by a button, crash, WHY ??<br>
  The same if the button create a new activity : code must be in OnCreate method or crash.  </p>
  
  <p>Thanks for help : need to return to basics of JAVA ?</p>