Android back button does not work while playing video in VideoView

The Android back button does not work during video playback in VideoView. But it works before playing the video. I use a special MediaController for VideoView.
I tried using dispatchKeyEvent , its not working.

Action code using VideoView:

mc = new CustomMediaController(mVideo.getContext(), screenIcon) { @Override public void hide(){ } @Override public boolean dispatchKeyEvent(KeyEvent event){ if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) { super.hide(); ((Activity) getContext()).finish(); return true; } return super.dispatchKeyEvent(event); } }; 


 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_BACK)) { onBackPressed(); finish(); } return true; } @Override public void onBackPressed() { super.onBackPressed(); finish(); } 


CustomMediaController also contains dispatchKeyEvent:

 @Override public boolean dispatchKeyEvent(KeyEvent event) { if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) { ((Activity) getContext()).finish(); } return super.dispatchKeyEvent(event); } 

Also, when I view the layout, mediaController did not save its position in the videoView. To fix this error, I added code to correct the position of the mediaController.

 FrameLayout f; RelativeLayout.LayoutParams lp; RelativeLayout.LayoutParams params; params = (RelativeLayout.LayoutParams) mVideo.getLayoutParams(); params.height = mp.getVideoHeight(); progress.dismiss(); f = (FrameLayout) mc.getParent(); lp = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); lp.addRule(RelativeLayout.ALIGN_BOTTOM, mVideo.getId()); try { ((LinearLayout) f.getParent()).removeView(f); } catch (Exception e) { ((RelativeLayout) f.getParent()).removeView(f); } ((RelativeLayout) mVideo.getParent()).addView(f, lp); //mc.setAnchorView(mVideo); mVideo.setLayoutParams(params); 
Button

Back works fine when I delete this code above.

I addressed the following issues in stack overflow, but did not solve the problem.

Stack Overflow Links I tried:

Android back button and MediaController
Back button will not work when VideoView plays video
The "First Button" button does not appear when playing a movie and videos
Back button problem in VideoView

+6
source share

All Articles