Video does not resume in VideoView

為{幸葍}努か 提交于 2019-12-11 11:53:52

问题


Many posts are there for this issue.But the solutions are available for when the activity gets paused. I tried all it doesn't work. My problem is little bit different

I have a videoview and when the user clicks the videoview ,video will be paused and if he clicks again it should be resumed.

My code snippet in ontouchlistener is,

   videopath = getIntent().getStringExtra("path");
    imageView = ((VideoView) findViewById(R.id.imageView));
    imageView.setVideoPath(videopath);
    imageView.start();
    imageView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(event.getAction()==MotionEvent.ACTION_UP)
                if (layout.getVisibility()==View.VISIBLE) {
                    imageView.seekTo(stopPosition);
                    imageView.resume();
                    layout.animate().translationY(-layout.getHeight()).setDuration(500);
                    layout.setVisibility(View.GONE);
                    hideSystemUI();
                } else {
                    imageView.pause();
                    stopPosition=imageView.getCurrentPosition();
                    showSystemUI();
                    layout.setVisibility(View.VISIBLE);
                    layout.animate().translationYBy(layout.getHeight());
                }
            return true;
        }
    });

I got this solution from this link . It doesn't work and simply using resume(); is also not working.


回答1:


This happens because your device looses the time you've stored. You can use MediaPlayer instance, Here how you can use,

    VideoView videoView;
    MediaPlayer mp;

    videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    this.mp = mp;
                }
            });

    public void pause(){
        //NOT videoview.pause();
        if (mp != null){
           mp.pause();
        }
    }

    public void resume(){
        //NOT videoview.resume();
        if (mp != null){
           mp.start();
        }   
    }

//This function will be implemented under onClick method
if (!videoView.isPlaying()) {
                 resume();
                 layout.setVisibility(View.GONE);
                 hideSystemUI();//hiding navigationbar
} 
else {//initially layout visibility is GONE
                 pause();
                 stopPosition=videoView.getCurrentPosition();
                 showSystemUI();
                 layout.setVisibility(View.VISIBLE);
                 layout.animate().translationYBy(layout.getHeight());
}


来源:https://stackoverflow.com/questions/47770267/video-does-not-resume-in-videoview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!