Android back button not working while playing video in VideoView
Asked Answered
D

1

11

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

Descendent answered 6/4, 2016 at 5:57 Comment(7)
@NarendraBaratam: I've edited my question. Provided the code also.Descendent
where is your onBackpressed(); in that activity??? Did you override that???Potence
@NarendraBaratam: ya. I tried onKeyDown & onBackPressed. I tried to finish the activity there.Descendent
you have to release() the media there..and stop()..before call super.onBackPressed() in onBackPressed()Potence
@NarendraBaratam: Can you tell me the usage ? mVideo.suspend(); ?. I didn't see release method for videoview. But suspend internally does the release operation I guess.Descendent
Let us continue this discussion in chat.Descendent
could you resolve this problem ?? i have the same situationTorment
L
0

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);
Loyalty answered 21/3, 2019 at 11:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.