Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.RuntimeException: setAudioSource failed

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.

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);
    }


}

By the way, I have included the uses-permission in manifext.xml file.

This is what i get in the Android Monitor:

  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)

I tried for some answers, didn't find any.

MY AndroidManifest.xml:

   <?xml version="1.0" encoding="utf-8"?>
      <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gk.audiocapture">
<uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

like image 782
Gokul Krishna Avatar asked May 18 '16 05:05

Gokul Krishna


3 Answers

I had the exact same problem and managed to fix it by asking for permission to record audio:

if (ActivityCompat.checkSelfPermission(activity(), Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {

    ActivityCompat.requestPermissions(activity(), new String[]{Manifest.permission.RECORD_AUDIO}, BuildDev.RECORD_AUDIO);

} else {

    startRecording();

}

where

BuildDev.RECORD_AUDIO is public static final int RECORD_AUDIO = 0;

like image 177
android enthusiast Avatar answered Nov 08 '22 09:11

android enthusiast


I am confused your title says java.lang.RuntimeException: setAudioSource failed and your stack trace says java.lang.RuntimeException: stop failed.

For java.lang.RuntimeException: setAudioSource failed you might be missing Runtime Permission.

You need to take Manifest.permission.RECORD_AUDIO from user.

public void onRecordBtnClicked() {
  if (ActivityCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO)
      != PackageManager.PERMISSION_GRANTED) {
    ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.RECORD_AUDIO },
        10);
  } else {
    recordAudio();
  }
}

private void recordAudio() {
  //Record Audio.
}

@Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
    @NonNull int[] grantResults) {
  super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  if (requestCode == 10) {
    if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
      recordAudio();
    }else{
      //User denied Permission.
    }
  }
}
like image 37
Kalpesh Patel Avatar answered Nov 08 '22 09:11

Kalpesh Patel


You're missing some breaks in your cases:

case R.id.start_button:
    startRecord();
    // You need to put a 'break;' here, or you'll fall through to the next case
case R.id.stop_button:
    stoprecord();
    // For the last case of a switch it might not matter, but it'd still be a good idea to put a 'break;' here as well
like image 35
Michael Avatar answered Nov 08 '22 09:11

Michael