MediaPlayer prepare Failed Error(1,-17) when using

2019-02-28 06:15发布

问题:

I am trying record from the AudioRecorder then I am trying to play the same with Media Player, But its giving mediaplayer.prepare() failed to prepare. Could any one tell me what could appropriate reason. Below is the code I am using

        public void onClick(View v) {

            recorder = new MediaRecorder();
            recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            if(case_value==0)
                recorder.setOutputFile(deviceMIC_path);
            else if(case_value==1)
                recorder.setOutputFile(deviceMIC_Handset_path);
            else if(case_value==2)
                recorder.setOutputFile(headsetMIC_path);

            recorder.setOnErrorListener(errorListener);
            recorder.setOnInfoListener(infoListener);

            try {
                recorder.prepare();
                recorder.start();
                if (ENABLE_LOGS)
                    Log.e(LOG_TAG, resources
                            .getString(R.string.record_started));
                Toast.makeText(context,
                        resources.getString(R.string.record_started),
                        Toast.LENGTH_LONG).show();
                recording = true;


            } catch (IllegalStateException e) {
                Log.e("","ILLEAGAL EXCEPTION...");e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
                Log.e("","IO EXCEPTION...");
            }
            catch(Exception e){
                e.printStackTrace();
                Log.e("","UNKNOWN EXCEPTION...");
            }

        }
    });

    stopButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            if (recording) {
                if (ENABLE_LOGS)
                    Log.e(LOG_TAG, "Record is going to be stoped...");
                recorder.stop();
                if (ENABLE_LOGS)
                    Log.e(LOG_TAG, "Record STOPPED....");
                Toast
                        .makeText(context, "Record STOPPED",
                                Toast.LENGTH_LONG).show();

                recorder.release();
                recorder=null;
            }
        }
    });

    playButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            MediaPlayer mediaplayer = new MediaPlayer();

            try {
                if(case_value==0)
                        mediaplayer.setDataSource(deviceMIC_path);
                else if(case_value==1)
                    mediaplayer.setDataSource(deviceMIC_Handset_path);
                else if(case_value==2)
                    mediaplayer.setDataSource(headsetMIC_path);


            } catch (IllegalArgumentException e) {

                e.printStackTrace();
            } catch (IllegalStateException e) {

                e.printStackTrace();
            } catch (IOException e) {

                e.printStackTrace();
            }
            try {
                mediaplayer.prepare();
            } catch (IllegalStateException e) {

                e.printStackTrace();
            } catch (IOException e) {

                e.printStackTrace();
            }
            catch(Exception e){
                e.printStackTrace();
            }
            // mAudioManager.setSpeakerphoneOn(false);
            if (ENABLE_LOGS)
                Log.e(LOG_TAG, "CASE VALUE : " + case_value);
            if (case_value == 0) {

                if (MainActivity.headsetState == 0) {

                    if (!mediaplayer.isPlaying()) {
                        mediaplayer.start();
                        if (ENABLE_LOGS)
                            Log.e(LOG_TAG, "PLAY started...");
                        Toast.makeText(context, "PLAY started...",
                                Toast.LENGTH_LONG).show();
                    } else {
                        mediaplayer.stop();
                    }

Below is the Error I am getting

12-12 12:00:01.186: E/com.brcm.factorymode.audioloop.AudioLoopFtmTestActivity(1860): Record started
12-12 12:00:04.467: E/com.brcm.factorymode.audioloop.AudioLoopFtmTestActivity(1860): Record is going to be stoped...
12-12 12:00:04.592: E/com.brcm.factorymode.audioloop.AudioLoopFtmTestActivity(1860): Record STOPPED....
12-12 12:00:06.710: W/MediaPlayer(1860): info/warning (1, 26)
12-12 12:00:06.725: E/MediaPlayer(1860): error (1, -17)
12-12 12:00:06.725: W/System.err(1860): java.io.IOException: Prepare failed.: status=0x1
12-12 12:00:06.725: W/System.err(1860):     at android.media.MediaPlayer.prepare(Native Method)
12-12 12:00:06.725: W/System.err(1860):     at com.brcm.factorymode.audioloop.AudioLoopFtmTestActivity$3.onClick(AudioLoopFtmTestActivity.java:155)
12-12 12:00:06.725: W/System.err(1860):     at android.view.View.performClick(View.java:2364)
12-12 12:00:06.725: W/System.err(1860):     at android.view.View.onTouchEvent(View.java:4179)
12-12 12:00:06.725: W/System.err(1860):     at android.widget.TextView.onTouchEvent(TextView.java:6534)
12-12 12:00:06.725: W/System.err(1860):     at android.view.View.dispatchTouchEvent(View.java:3709)
12-12 12:00:06.725: W/System.err(1860):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:883)
12-12 12:00:06.725: W/System.err(1860):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:883)
12-12 12:00:06.725: W/System.err(1860):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:883)
12-12 12:00:06.725: W/System.err(1860):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:883)
12-12 12:00:06.725: W/System.err(1860):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:883)
12-12 12:00:06.725: W/System.err(1860):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
12-12 12:00:06.725: W/System.err(1860):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
12-12 12:00:06.725: W/System.err(1860):     at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
12-12 12:00:06.725: W/System.err(1860):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
12-12 12:00:06.725: W/System.err(1860):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
12-12 12:00:06.725: W/System.err(1860):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-12 12:00:06.725: W/System.err(1860):     at android.os.Looper.loop(Looper.java:123)
12-12 12:00:06.733: W/System.err(1860):     at android.app.ActivityThread.main(ActivityThread.java:4338)
12-12 12:00:06.741: W/System.err(1860):     at java.lang.reflect.Method.invokeNative(Native Method)
12-12 12:00:06.741: W/System.err(1860):     at java.lang.reflect.Method.invoke(Method.java:521)
12-12 12:00:06.741: W/System.err(1860):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
12-12 12:00:06.741: W/System.err(1860):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
12-12 12:00:06.741: W/System.err(1860):     at dalvik.system.NativeStart.main(Native Method)
12-12 12:00:06.741: E/com.brcm.factorymode.audioloop.AudioLoopFtmTestActivity(1860): CASE VALUE : 0
12-12 12:00:06.749: E/MediaPlayer(1860): start called in state 0
12-12 12:00:06.749: E/MediaPlayer(1860): error (-38, 0)
12-12 12:00:06.749: E/com.brcm.factorymode.audioloop.AudioLoopFtmTestActivity(1860): PLAY started...
12-12 12:00:06.756: I/MediaPlayer(1860): Info (1,26)
12-12 12:00:06.756: E/MediaPlayer(1860): Error (-38,0)

Could any one please tell me the way to resolve this problem.

回答1:

Implement the OnCompletionListener and register it with your MediaPlayer instance.

After the media has been played out it will call this callback method onCompletion

void onCompletion(MediaPlayer mp){
   //Here you stop it.
   mp.stop();
   //Reset the data source path to the new file
   mp.setDataSource(<uri>);
   mp.prepare(); // or mp.prepareAsync();
   // start the mediaplayer after the prepare has completed.
}

Release the mediaplayer instance once you are done playing all the files.