释放Mediaplayer并停止onPause和onResume会在Android中产生错误 [英] Releasing mediaplayer and stopping it onPause and onResume gives error in Android

查看:276
本文介绍了释放Mediaplayer并停止onPause和onResume会在Android中产生错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用videoView和mediaplayer但在onPause和onResume中停止mediaplayer会给我错误:

i m using videoView and mediaplayer but stopping mediaplayer in onPause and onResume gives me error:

 static MediaPlayer mediaPlayer;
 private VideoViewCustom videoView;
 @Override
    public void onCreate(final Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detailvideo);
        context = this;
        videoView = (VideoViewCustom) findViewById(R.id.videoplayer);
        //initialize media player
        mediaPlayer = new MediaPlayer();
        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            // Close the progress bar and play the video
            public void onPrepared(MediaPlayer mp) {
                Log.d(TAG,"setOnPreparedListener");
                mediaPlayer = mp;
                int timeDuration = 0;
                boolean videoPlaying = false;
                if(savedInstanceState != null) {
                    Log.d(TAG,"savedInstanceState != null");
                    timeDuration = savedInstanceState.getInt("timeduration");
                    videoPlaying = savedInstanceState.getBoolean("videoPlaying");
                    Log.d(TAG, "timeDuration saved:" + timeDuration);
                    Log.d(TAG, "videoPlaying saved:" + videoPlaying);
                    Log.d(TAG,"video position:"+videoView.getCurrentPosition());
                    if (videoPlaying) {
                        videoView.seekTo(timeDuration);
                        mediaPlayer.start();
                        videoView.start();
                    } else {
                        videoView.seekTo(timeDuration);
                    }
                }
                else
                {
                    Log.d(TAG, "savedInstanceState == null");
                    mediaPlayer.start();
                    videoView.start();
                }

                finalTime = videoView.getDuration();
                Log.d(TAG, "mp.getCurrentPosition():" + videoView.getCurrentPosition());
                Date d = new Date(videoView.getCurrentPosition());
                SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
                String time = sdf.format(d).toString();
                Log.d(TAG, "time:" + time);
                videoSeekBar.setProgress(timeDuration);
                videoSeekBar.setMax(finalTime);
               durationHandler.postDelayed(updateSeekBarTime,1000);
            }
        });
}
 @Override
    public void onResume() {
        super.onResume();
        // Setup the player
        videoView.resume();
    }
    @Override
    public void onPause()
    {
        if(mediaPlayer != null)
        {
            mediaPlayer.stop();
        }
        super.onPause();

    }
@Override
    public void onDestroy() {
        if(mediaPlayer != null) {
            mediaPlayer.release();
        }
        super.onDestroy();
    }
 backArrowImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mediaPlayer != null)

                {
                    mediaPlayer.stop();
                    mediaPlayer.release();
                }
                finish();
            }
        });
}

一旦我按下backArrowImageView按钮,我就在我的应用上方编写了代码崩溃,并给了我这个日志记录:

Caused by: java.lang.IllegalStateException
        at android.media.MediaPlayer._stop(Native Method)
        at android.media.MediaPlayer.stop(MediaPlayer.java:1561)
        at com.ui.VideoDetailActivity.onPause(VideoDetailActivity.java:493)
        at android.app.Activity.performPause(Activity.java:5686)
        at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1239)
        at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3400)
            at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3369)
            at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3347)
            at android.app.ActivityThread.access$1100(ActivityThread.java:171)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1326)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5679)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
            at dalvik.system.NativeStart.main(Native Method)

VideoDetailActivity.java:493  contains:  
         mediaPlayer.stop();      inside onPause method ?

推荐答案

根据Android文档:

According to the Android documentation:

"IllegalStateException,如果内部播放器引擎尚未建立 已初始化或已被释放."

"IllegalStateException if the internal player engine has not been initialized or has been released."

因此,请确保已初始化MediaPlayer,并且不要使用已发布的MediaPlayer. 在onPause()和onDestroy()中使用以下方式,

So ensure your MediaPlayer is initialized, and you don't use the released one. Use like below in onPause() and onDestroy(),

try{
    if(mediaPlayer !=null && mediaPlayer.isPlaying()){
       Log.d("TAG------->", "player is running");
       mediaPlayer.stop();
       Log.d("Tag------->", "player is stopped");
       mediaPlayer.release();
       Log.d("TAG------->", "player is released");
    }
}catch(Exception e){
}

这篇关于释放Mediaplayer并停止onPause和onResume会在Android中产生错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆