其他分享
首页 > 其他分享> > 使用记录的文件.3gp时,MediaPlayer准备失败错误(1,-17)

使用记录的文件.3gp时,MediaPlayer准备失败错误(1,-17)

作者:互联网

我正在尝试从AudioRecorder录制,然后尝试与Media Player播放相同的文件,但是它给mediaplayer.prepare()的准备失败.任何人都可以告诉我什么合适的理由.
下面是我正在使用的代码

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

以下是我遇到的错误

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)

任何人都可以告诉我解决此问题的方法.

解决方法:

实现OnCompletionListener并将其注册到MediaPlayer实例.

播放完媒体后,它将在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.
}

播放完所有文件后,释放mediaplayer实例.

标签:android,media-player,audio-recording
来源: https://codeday.me/bug/20191014/1912304.html