MediaRecorder IOException: prepare failed

2019-07-04 07:55发布

问题:

I want to use MediaRecorder to record voice, my code is:

 public void record(View v) {
       Log.d(TAG, "record");

    this.mediaRecorder.setAudioChannels(1);
    this.mediaRecorder.setAudioSamplingRate(44100);
    this.mediaRecorder.setAudioEncodingBitRate(64000);
    this.mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    this.mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    this.mediaRecorder.setOutputFile(this.file.getAbsolutePath());
    this.mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
    try {
        this.mediaRecorder.prepare();
        this.mediaRecorder.start();

        // update the buttons
        this.setButtonsEnabled(false, true, false);
    } catch (IOException e) {
        Log.e(TAG, "Failed to record()", e);
    }
}

Or

   public void record(View v) {
    Log.d(TAG, "record");
    this.mediaRecorder = new MediaRecorder();
    this.mediaRecorder.setAudioChannels(1);
    this.mediaRecorder.setAudioSamplingRate(8000);

    this.mediaRecorder.setAudioEncodingBitRate(16);
    this.mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    this.mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);
    this.mediaRecorder.setOutputFile(this.file.getAbsolutePath());

    this.mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

    try {
        this.mediaRecorder.prepare();
        this.mediaRecorder.start();

        // update the buttons
        this.setButtonsEnabled(false, true, false);
    } catch (IOException e) {
        Log.e(TAG, "Failed to record()", e);
    }
}

On a Samsung all is OK, but on a Dell two methods do not succeed

Here is logcat:

 02-01 13:56:51.094: E/AudioRecorderDemoActivity(1397): Failed to record()
 02-01 13:56:51.094: E/AudioRecorderDemoActivity(1397): java.io.IOException: prepare failed.
 02-01 13:56:51.094: E/AudioRecorderDemoActivity(1397):     at android.media.MediaRecorder._prepare(Native Method)
 02-01 13:56:51.094: E/AudioRecorderDemoActivity(1397):     at android.media.MediaRecorder.prepare(MediaRecorder.java:524)
 02-01 13:56:51.094: E/AudioRecorderDemoActivity(1397):     at com.marakana.android.audiorecorderdemo.AudioRecorderDemoActivity.record(AudioRecorderDemoActivity.java:69)
 02-01 14:05:20.074: E/AndroidRuntime(1790): FATAL EXCEPTION: main
 02-01 14:05:20.074: E/AndroidRuntime(1790): java.lang.IllegalStateException: Could not execute method of the activity

回答1:

First at all you code looks fine. Have you added the required permissions to your manifest file?

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />

If yes, then try replacing:

this.mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);

by

this.mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);

Don't forget to check if the path of your video file is correct.



回答2:

This is a big problem but has a very small solution

In most cases, the filename that we get from this.file.getAbsolutePath() contains file:/// as a prefix

    ////////////////////////////////////////////////* INCORRECT CODE */
    this.mediaRecorder.setOutputFile(this.file.getAbsolutePath());
    /*the above line sets a file url beginning with a "file:///"
    //however, since this setOutputFile requires us to send a
    //string referring to the uri, we will have to get rid of the
    //"file:///" and simply write the uri */
    ////////////////////////////////////////////////* CORRECTED CODE BELOW */
    this.mediaRecorder.setOutputFile(this.file.getAbsolutePath().substring(8));
    /*the above line of code extracts the string uri eliminating
    // file:/// */

Hope you find this answer helpful



回答3:

This Exception will be raised . if any of the following things failed:

file not found: Ensure that output file location that yu have specified is existing, otherwise it will throw you filenotfoundexception

Write Permission: You must specify Write permission in your manifest file.

Record permission : specify Record permission in your manifest file.

you can use this..

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>

Still you get error.. try display the error. Like this

try{
        mRecorder.prepare();

    } catch (IOException e) {
        Log.e(LOG_TAG, "prepare() failed");
       System.out.println(""+e);    //to display the error
    }

mRecorder.start();



回答4:

I deleted

this.mediaRecorder.setAudioEncodingBitRate(16);

at method 2 and now it's working.