Videoview按下返回键与Home键后停止播放视频问题求助
Hey there! I’ve run into this exact issue with VideoView before—let’s break down why it’s happening and how to fix it.
Why does this happen?
By default, when you press the Home button, your Activity enters the onPause() state, and VideoView automatically pauses playback. If you press the Back button, your Activity gets destroyed (onDestroy()), so VideoView stops entirely since it’s tightly tied to the Activity’s lifecycle.
Solutions based on what you want to achieve:
1. Resume playback when returning to the app (after Home button)
If you just want the video to pick up where it left off when you come back to your Activity, you need to save the playback position and restore it in the lifecycle methods.
First, adjust your Activity to track the position with a member variable:
private int currentVideoPosition = 0; private VideoView videoview; // Make this a member variable instead of local to onCreate()
Then update your lifecycle methods and onCreate logic:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.your_layout); videoview = findViewById(R.id.videoview); Uri uri = android.net.Uri.parse("xxxx.xxxx"); videoview.setVideoURI(uri); // Restore position if we're coming back after a configuration change or pause if (savedInstanceState != null) { currentVideoPosition = savedInstanceState.getInt("videoPosition"); videoview.seekTo(currentVideoPosition); } videoview.start(); } @Override protected void onPause() { super.onPause(); // Save the current position before pausing if (videoview != null && videoview.isPlaying()) { currentVideoPosition = videoview.getCurrentPosition(); videoview.pause(); } } @Override protected void onResume() { super.onResume(); // Resume playback from the saved position if (videoview != null && currentVideoPosition > 0) { videoview.seekTo(currentVideoPosition); videoview.start(); } } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); // Save the position so we can restore it if the Activity is recreated outState.putInt("videoPosition", currentVideoPosition); }
2. Customize Back button behavior (don’t stop playback immediately)
If you want the Back button to pause the video instead of closing the Activity, override onBackPressed():
@Override public void onBackPressed() { if (videoview != null && videoview.isPlaying()) { // Pause the video instead of exiting videoview.pause(); // Optionally show a toast or message to let the user know playback is paused } else { // If video is already paused, proceed with normal back navigation super.onBackPressed(); } }
3. Play audio in the background (video won’t show, but audio continues)
VideoView is tied to the Activity’s UI, so it can’t run in the background. If you need audio to keep playing when the app is minimized, you’ll need to use a Service with MediaPlayer instead. Here’s a quick outline:
- Create a
Servicethat manages theMediaPlayerinstance - Pass the video URI to the Service and start playback there
- Bind the Service to your Activity to control playback (pause/play)
- The Service will keep running even when the Activity is destroyed, so audio continues
Key Notes
Always release VideoView resources in onDestroy() to avoid memory leaks:
@Override protected void onDestroy() { super.onDestroy(); if (videoview != null) { videoview.stopPlayback(); } }
内容的提问来源于stack exchange,提问作者D.J

