Android back button not working while playing video in VideoView

自闭症网瘾萝莉.ら 提交于 2019-12-10 12:39:09

问题


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


回答1:


After override the dispatchKeyEvent() in my mediacontroller it works fine

 mediaController=new MediaController(this){
        @Override
        public void hide() {
            mediaController.show();
        }
        @Override
        public boolean dispatchKeyEvent(KeyEvent event){
            if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
                super.hide();
                ((Activity) getContext()).finish();
                return true;
            }
            return super.dispatchKeyEvent(event);
        }
    };

    videoView.setMediaController(mediaController);


来源:https://stackoverflow.com/questions/36442678/android-back-button-not-working-while-playing-video-in-videoview

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