Java – videoview ontouch event: pause / resume video and show / hide mediacontroller and actionbar
Problem summary:
1) How do I pause the video first instead of playing it now?
2) How to pause / UN pause video touch, you can also hide / show actionbar and mediacontroller
I would appreciate any suggestions thank you! (relevant specifications attached)
Update 1
Find a solution to question 2 (need to return false), but I still don't know how to answer question 1
When users press a button in my app, they need to watch their video When they first opened the screen, I wanted the video to pause and not play immediately I also want to pause the video by clicking on the screen When the video pauses, I want to display actionbar and mediacontroller When the video is restored, I want to hide the actionbar and mediacontroller (maybe a little delayed)?
I've tried some things, but I finally encountered problems, such as the video will pause but can't be restored, or the actionbar and mediacontroller won't display or hide normally
Update 2
I have found a partial solution to problem 1 and updated the code to show the video as paused when I first open it But when it was first turned on, it only showed a black screen until I touched the video to play it After watching the video once, it will restart and pause, wait for playback again, and display the correct image from the video But I didn't know how to get rid of the black screen at the beginning
Relevant codes:
public class ViewImageVideoFragment extends Fragment { private int position = 0; private MediaController mMediaController; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mMediaController = new MediaController(getActivity()); ... } @Override public View onCreateView(LayoutInflater inflater,ViewGroup parent,Bundle savedInstanceState) { if (savedInstanceState != null) { position = savedInstanceState.getInt("position"); } View v = inflater.inflate(R.layout.fragment_video_view,parent,false); mVideoView = (VideoView) v.findViewById(R.id.fragmentVideoView); mVideoView.setVideoPath(videoPath); mVideoView.setMediaController(mMediaController); mVideoView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v,MotionEvent motionEvent) { if (mVideoView.isPlaying()) { mVideoView.pause(); if (!getActivity().getActionBar().isShowing()) { getActivity().getActionBar().show(); mMediaController.show(0); } position = mVideoView.getCurrentPosition(); return false; } else { if (getActivity().getActionBar().isShowing()) { getActivity().getActionBar().hide(); mMediaController.hide(); } mVideoView.seekTo(position); mVideoView.start(); return false; } } }); mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mediaPlayer) { mVideoView.seekTo(0); } }); if (position != 0) { mVideoView.seekTo(position); mVideoView.start(); } else { mVideoView.seekTo(0); } } @Override public void onSaveInstanceState(Bundle savedInstanceState) { super.onSaveInstanceState(savedInstanceState); if (mVideoView != null) { savedInstanceState.putInt("position",mVideoView.getCurrentPosition()); } mVideoView.pause(); } }
Solution
To pause the video first, just change seekto (0) to seekto (1) in the code This will move the video to 1 millisecond, where you can take it away
//edited here private int position = 1; private MediaController mMediaController; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mMediaController = new MediaController(getActivity()); ... } @Override public View onCreateView(LayoutInflater inflater,Bundle savedInstanceState) { if (savedInstanceState != null) { position = savedInstanceState.getInt("position"); } View v = inflater.inflate(R.layout.fragment_video_view,false); mVideoView = (VideoView) v.findViewById(R.id.fragmentVideoView); mVideoView.setVideoPath(videoPath); mVideoView.setMediaController(mMediaController); mVideoView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v,MotionEvent motionEvent) { if (mVideoView.isPlaying()) { mVideoView.pause(); if (!getActivity().getActionBar().isShowing()) { getActivity().getActionBar().show(); mMediaController.show(0); } position = mVideoView.getCurrentPosition(); return false; } else { if (getActivity().getActionBar().isShowing()) { getActivity().getActionBar().hide(); mMediaController.hide(); } mVideoView.seekTo(position); mVideoView.start(); return false; } } }); mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mediaPlayer) { //here mVideoView.seekTo(1); } }); //here if (position != 1) { mVideoView.seekTo(position); mVideoView.start(); } else { //here mVideoView.seekTo(1); } } @Override public void onSaveInstanceState(Bundle savedInstanceState) { super.onSaveInstanceState(savedInstanceState); if (mVideoView != null) { savedInstanceState.putInt("position",mVideoView.getCurrentPosition()); } mVideoView.pause(); }
}