title: setAudioSource failed in Android Studio sample code
body: <p>I'm new to Android and Java. </p>
  
  <p>I'm using the latest Android Studio on my windows 10. </p>
  
  <p>I simply created an empty activity and pasted the code from</p>
  
  <p><a href="https://developer.android.com/guide/topics/media/audio-capture.html" rel="nofollow">https://developer.android.com/guide/topics/media/audio-capture.html</a> </p>
  
  <p>to my MainActivity. </p>
  
  <p>Following its comments, I also added uses-permission in my manifest. </p>
  
  <p>I've read all Q&amp;As, I believe the manifest is good. </p>
  
  <p>However I keep getting setAudioSource failed with the callstack in the end of post.</p>
  
  <p>The error is the same on both my Nexus 5 and my Nexus 5 Virtual device.</p>
  
  <p>Could you help me, please? Thanks!</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.kyang.myapplication"&gt;
      &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
      &lt;uses-permission android:name="android.permission.RECORD_AUDIO" /&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;
  &lt;/manifest&gt;
  </code></pre>
  
  <p>My code is below:</p>
  
  <pre><code>package com.example.kyang.myapplication;
  
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  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 MainActivity 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 MainActivity() {
          mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
          mFileName += "/audiorecordtest.3gp";
      }
  
  
      @Override
      public void onPause() {
          super.onPause();
          if (mRecorder != null) {
              mRecorder.release();
              mRecorder = null;
          }
  
          if (mPlayer != null) {
              mPlayer.release();
              mPlayer = null;
          }
      }
  
      @Override
      protected 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);
      }
  }
  </code></pre>
  
  <p>Output in Android Monitor Verbose mode:</p>
  
  <pre><code>05-20 09:34:18.552 15335-15335/com.example.kyang.myapplication I/Process: Sending signal. PID: 15335 SIG: 9
  05-20 09:35:07.850 16251-16251/com.example.kyang.myapplication W/ActivityThread: Application com.example.kyang.myapplication is waiting for the debugger on port 8100...
  05-20 09:35:07.854 16251-16251/com.example.kyang.myapplication I/System.out: Sending WAIT chunk
  05-20 09:35:08.273 16251-16257/com.example.kyang.myapplication I/art: Debugger is active
  05-20 09:35:08.468 16251-16251/com.example.kyang.myapplication I/System.out: Debugger has connected
  05-20 09:35:08.468 16251-16251/com.example.kyang.myapplication I/System.out: waiting for debugger to settle...
  05-20 09:35:08.669 16251-16251/com.example.kyang.myapplication I/System.out: waiting for debugger to settle...
  05-20 09:35:08.872 16251-16251/com.example.kyang.myapplication I/System.out: waiting for debugger to settle...
  05-20 09:35:09.086 16251-16251/com.example.kyang.myapplication I/System.out: waiting for debugger to settle...
  05-20 09:35:09.287 16251-16251/com.example.kyang.myapplication I/System.out: waiting for debugger to settle...
  05-20 09:35:09.488 16251-16251/com.example.kyang.myapplication I/System.out: waiting for debugger to settle...
  05-20 09:35:09.690 16251-16251/com.example.kyang.myapplication I/System.out: waiting for debugger to settle...
  05-20 09:35:09.891 16251-16251/com.example.kyang.myapplication I/System.out: waiting for debugger to settle...
  05-20 09:35:10.092 16251-16251/com.example.kyang.myapplication I/System.out: waiting for debugger to settle...
  05-20 09:35:10.293 16251-16251/com.example.kyang.myapplication I/System.out: debugger has settled (1377)
  05-20 09:35:10.310 16251-16251/com.example.kyang.myapplication W/System: ClassLoader referenced unknown path: /data/app/com.example.kyang.myapplication-2/lib/x86_64
  05-20 09:35:10.571 16251-16251/com.example.kyang.myapplication W/System: ClassLoader referenced unknown path: /data/app/com.example.kyang.myapplication-2/lib/x86_64
  05-20 09:35:10.883 16251-16304/com.example.kyang.myapplication D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
  
                                                                                   [ 05-20 09:35:10.893 16251:16251 D/         ]
                                                                                   HostConnection::get() New Host Connection established 0x7fdfb5ffea40, tid 16251
  
  
                                                                                   [ 05-20 09:35:10.940 16251:16304 D/         ]
                                                                                   HostConnection::get() New Host Connection established 0x7fdfb9ac6400, tid 16304
  05-20 09:35:10.976 16251-16304/com.example.kyang.myapplication I/OpenGLRenderer: Initialized EGL, version 1.4
  05-20 09:35:12.406 16251-16251/com.example.kyang.myapplication D/AndroidRuntime: Shutting down VM
  
  
                                                                                   --------- beginning of crash
  05-20 09:35:12.410 16251-16251/com.example.kyang.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                   Process: com.example.kyang.myapplication, PID: 16251
                                                                                   java.lang.RuntimeException: setAudioSource failed.
                                                                                       at android.media.MediaRecorder.setAudioSource(Native Method)
                                                                                       at com.example.kyang.myapplication.MainActivity.startRecording(MainActivity.java:65)
                                                                                       at com.example.kyang.myapplication.MainActivity.onRecord(MainActivity.java:33)
                                                                                       at com.example.kyang.myapplication.MainActivity.access$000(MainActivity.java:20)
                                                                                       at com.example.kyang.myapplication.MainActivity$RecordButton$1.onClick(MainActivity.java:90)
                                                                                       at android.view.View.performClick(View.java:5198)
                                                                                       at android.view.View$PerformClick.run(View.java:21147)
                                                                                       at android.os.Handler.handleCallback(Handler.java:739)
                                                                                       at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                       at android.os.Looper.loop(Looper.java:148)
                                                                                       at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
  </code></pre>