Android退出Activity时停止ExoPlayer视频播放的技术求助
Hey there! I totally get your frustration—having a video keep playing in the background after you've exited the app is super annoying. Let's fix this step by step, using your existing code as a reference.
Why This Happens
Right now, you're creating a new ExoPlayer instance inside your RecyclerView Adapter for each video, but you're never properly stopping or releasing these players. When you exit the Activity, these player instances are still alive and holding onto resources (like audio focus), so they keep playing.
Step 1: Track Player Instances in Your Adapter
First, let's modify your Adapter to keep track of all active players, so we can clean them up later.
- Add a list to your Adapter class to store players:
private List<ExoPlayer> activePlayers = new ArrayList<>();
- Update your ViewHolder to hold a reference to its player:
public class YourVideoViewHolder extends RecyclerView.ViewHolder { // Your existing views: videoFrame, playVideo, userVideoUpdate, etc. ExoPlayer player; // Add this line public YourVideoViewHolder(View itemView) { super(itemView); // Initialize all your views here } }
- When creating the player in your
onBindViewHolder, assign it to the ViewHolder and add it to the tracking list:
// Replace your existing player initialization with this holder.player = ExoPlayerFactory.newSimpleInstance(activity, trackSelector); activePlayers.add(holder.player); // Use holder.player for all subsequent operations DefaultHttpDataSourceFactory defaultHttpDataSourceFactory = new DefaultHttpDataSourceFactory("exo player"); ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory(); final MediaSource mediaSource = new ExtractorMediaSource(uri, defaultHttpDataSourceFactory, extractorsFactory, null, null); holder.playVideo.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { holder.playVideo.setVisibility(View.GONE); holder.userVideoUpdate.setPlayer(holder.player); holder.player.prepare(mediaSource); holder.player.setPlayWhenReady(true); holder.volumeUp.setVisibility(View.VISIBLE); return true; } });
Step 2: Clean Up Players When ViewHolders Are Recycled
RecyclerView reuses ViewHolders, so we need to release the player when a holder is no longer in use:
@Override public void onViewRecycled(@NonNull YourVideoViewHolder holder) { super.onViewRecycled(holder); if (holder.player != null) { holder.player.stop(); holder.player.release(); activePlayers.remove(holder.player); holder.player = null; } }
Step 3: Release All Players When the Activity Destroys
Finally, add a method to your Adapter to release all remaining players, and call it from your Activity's onDestroy method:
- Add this method to your Adapter:
public void releaseAllPlayers() { for (ExoPlayer player : activePlayers) { if (player != null) { player.stop(); player.release(); } } activePlayers.clear(); }
- In your Activity, override
onDestroy:
@Override protected void onDestroy() { super.onDestroy(); // Replace "yourVideoAdapter" with the actual name of your Adapter instance yourVideoAdapter.releaseAllPlayers(); }
Key Notes
- Always call
release()on ExoPlayer when you're done with it—this frees up all system resources (including audio focus) and stops playback completely. - Combining recycling cleanup with Activity destruction ensures no players are left running in the background.
内容的提问来源于stack exchange,提问作者vagish dixit

