title: java.lang.RuntimeException: setAudioSource failed
body: <p>I am new to android development. I am just trying to record an audio with android studio(2.1.1) testing with 6.0.1 Marshmallow device.</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
      Button start, stop;
      public MediaRecorder recorder = null;
      public String fileextn = ".mp4";
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          start = (Button) findViewById(R.id.start_button);
          stop = (Button) findViewById(R.id.stop_button);
  
          start.setOnClickListener(new View.OnClickListener(){
  
                                       @Override
                                       public void onClick(View v) {
  
                                           switch (v.getId()) {
                                               case R.id.start_button:
                                                   startRecord();
                                               case R.id.stop_button:
                                                   stoprecord();
                                           }
                                       }
  
                                   }
          );
  
      }
  
      public void startRecord() {
          recorder = new MediaRecorder();
          recorder.reset();
          recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
          recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
          recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
          recorder.setOutputFile(getFilePath());
  
          try {
              recorder.prepare();
              recorder.start();
          } catch (IllegalStateException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  
      public void stoprecord() {
  
          if (recorder != null) {
              recorder.stop();
              recorder.reset();
  
              recorder.release();
              recorder = null;
          }
  
      }
  
  
      private String getFilePath() {
  
          String filepath = Environment.getExternalStorageDirectory().getPath();
          File file = new File(filepath, "MediaRecorderSample");
  
          if (!file.exists())
              file.mkdirs();
  
          return (file.getAbsolutePath() + "/" + fileextn);
      }
  
  
  }
  </code></pre>
  
  <p>By the way, I have included the uses-permission in manifext.xml file. </p>
  
  <p><strong>This is what i get in the Android Monitor:</strong> </p>
  
  <pre><code>  05-18 11:08:36.576 10414-10414/com.example.gk.audiocapture E/AndroidRuntime: FATAL EXCEPTION: main
                                                                         Process: com.example.gk.audiocapture, PID: 10414
                                                                           java.lang.RuntimeException: stop failed.
                                                                               at android.media.MediaRecorder.stop(Native Method)
                                                                               at com.example.gk.audiocapture.MainActivity.stoprecord(MainActivity.java:65)
                                                                               at com.example.gk.audiocapture.MainActivity$1.onClick(MainActivity.java:35)
                                                                               at android.view.View.performClick(View.java:5207)
                                                                               at android.view.View$PerformClick.run(View.java:21168)
                                                                               at android.os.Handler.handleCallback(Handler.java:746)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                               at android.os.Looper.loop(Looper.java:148)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5443)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
  </code></pre>
  
  <p>I tried for some answers, didn't find any.</p>
  
  <p>MY AndroidManifest.xml: </p>
  
  <pre><code>   &lt;?xml version="1.0" encoding="utf-8"?&gt;
        &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example.gk.audiocapture"&gt;
  &lt;uses-permission android:name="android.permission.RECORD_AUDIO"&gt;&lt;/uses-permission&gt;
  &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"&gt;&lt;/uses-permission&gt;
  &lt;application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:supportsRtl="true"
      android:theme="@style/AppTheme"&gt;
      &lt;activity android:name=".MainActivity"&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;
  </code></pre>
  
  <p></p>