Android back button is not working while playing the video in VideoView. But it works before playing the video. I am using a custom MediaController for VideoView.
I tried using dispatchKeyEvent, its not working.
Code of Activity that am 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 scroll the layout, mediaController was not maintaining its position in videoView. To resolve this bug, I've added a code to fix the mediaController position.
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);
Back button works perfect when I remove this above code.
I went through the following questions in stack overflow itself but didn't resolved my issue.
Stack Overflow links I tried:
Android back button and MediaController
Back button won't work when VideoView is playing video
First Back button press not caught when playing a video android
Problem with back button in VideoView