Back button problem in VideoView

It’s hard for me to get the back button to actually complete my work when I click. I start a very simple video review using progressdialog to show the download dialog and onpreparedlistener, etc. Etc. Simple things. In any case, at present, when I press the "Back" button, it just cancels the progress of the dialogue, and leaves a black screen, and then presses again, the dialogue restarts! and then when I press the back button again, the video cannot play back dialog box appears. very annoying. Thank you for your help.

public class VideoActivity extends Activity { private VideoView mVideoView; private static ProgressDialog progressdialog; private String path; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.videoview); progressdialog = ProgressDialog.show(this, "", " Video Loading...", true); progressdialog.setCancelable(true); mVideoView = (VideoView) findViewById(R.id.surface_view); mVideoView.setMediaController(new MediaController(this)); Bundle b = this.getIntent().getExtras(); path = b.getString("path"); mVideoView.setVideoURI(Uri.parse(path)); mVideoView.setOnPreparedListener(new OnPreparedListener() { public void onPrepared(MediaPlayer mp) { progressdialog.dismiss(); mVideoView.requestFocus(); mVideoView.start(); } }); } @Override public void onBackPressed() { // TODO Auto-generated method stub super.onBackPressed(); super.finish(); } } 
+4
source share
5 answers

You can simply write: (No need to create a new class for MediaController)

 mVideoView.setMediaController(new MediaController(this){ public boolean dispatchKeyEvent(KeyEvent event) { if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) ((Activity) getContext()).finish(); return super.dispatchKeyEvent(event); } }); 
+13
source

You need to create your own MediaController class and override the dispatchKeyEvent function in order to capture the inverse KeyEvent and let the action know.

See Android back button and MediaController for more details.

 public class CustomMediaController extends MediaController { public CustomMediaController(Context context, AttributeSet attrs) { super(context, attrs); } public CustomMediaController(Context context, boolean useFastForward) { super(context, useFastForward); } public CustomMediaController(Context context) { super(context, true); } public boolean dispatchKeyEvent(KeyEvent event) { if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) ((Activity) getContext()).finish(); return super.dispatchKeyEvent(event); } } 
+2
source

From CommansWare

Based on the source code, this should work:

  • Extend MediaController (call RonnieMediaController for this answer)
  • Override KeyEvent () dispatcher in RonnieMediaController
  • Before binding to the superclass, check KeyEvent.KEYCODE_BACK, and if this happens, inform your activity about completion ()
  • Use RonnieMediaController instead of MediaController with your VideoView

Personally, I will just leave it alone, since with this change your user cannot make RonnieMediaController disappear on demand.

Here is the link to the original post.

0
source

finish () does not kill your activity, it just signals Android that it no longer needs to run Activity.

I remember how this could be solved by putting the "return" in the right places.

-2
source
 public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { System.exit(0); } return false; } 
-2
source

All Articles