视频播放器开发:点击FAB按钮播放上次播放视频的实现咨询
Hey Ashok, great question! Implementing this feature is pretty straightforward once you break it down into two core parts: saving the last playback state and restoring that state when the FAB is tapped. Let’s walk through each step with code examples tailored to Android (since you mentioned a FAB, I’m assuming this is an Android app).
1. Save the Last Playback State
First, you need to store key details about the last played video whenever the user exits the app, pauses playback, or finishes watching. The easiest way to do this is using SharedPreferences—it’s perfect for lightweight, persistent app data.
You’ll want to save:
- The URI/path of the video file
- The current playback position (in milliseconds)
Add this code wherever you need to save the state (e.g., in onPause(), onStop(), or when the user closes the video):
// Get SharedPreferences instance val sharedPrefs = getSharedPreferences("VideoPlayerPrefs", Context.MODE_PRIVATE) val editor = sharedPrefs.edit() // Save video URI (replace with your actual video URI) editor.putString("last_video_uri", currentVideoUri.toString()) // Save current playback position (replace with your player's current position) editor.putLong("last_playback_position", videoPlayer.currentPosition) // Commit the changes editor.apply()
If you’re using Java, here’s the equivalent code:
SharedPreferences sharedPrefs = getSharedPreferences("VideoPlayerPrefs", Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPrefs.edit(); editor.putString("last_video_uri", currentVideoUri.toString()); editor.putLong("last_playback_position", videoPlayer.getCurrentPosition()); editor.apply();
2. Restore Playback When Tapping the FAB
Next, set up your FAB’s click listener to fetch the saved state, load the video, and jump to the last playback position. Make sure to handle cases where there’s no saved video (e.g., first launch).
Here’s how to implement the FAB click logic:
Kotlin Example:
fab.setOnClickListener { val sharedPrefs = getSharedPreferences("VideoPlayerPrefs", Context.MODE_PRIVATE) // Retrieve saved data val lastVideoUriString = sharedPrefs.getString("last_video_uri", null) val lastPosition = sharedPrefs.getLong("last_playback_position", 0) if (lastVideoUriString != null) { val lastVideoUri = Uri.parse(lastVideoUriString) // Load the video into your player (replace with your player's initialization logic) videoPlayer.setDataSource(this, lastVideoUri) videoPlayer.prepare() // Jump to the last playback position videoPlayer.seekTo(lastPosition) // Start playback videoPlayer.start() } else { // No saved video—show a message or open a file picker Toast.makeText(this, "No recent video found", Toast.LENGTH_SHORT).show() } }
Java Example:
fab.setOnClickListener(v -> { SharedPreferences sharedPrefs = getSharedPreferences("VideoPlayerPrefs", Context.MODE_PRIVATE); String lastVideoUriString = sharedPrefs.getString("last_video_uri", null); long lastPosition = sharedPrefs.getLong("last_playback_position", 0); if (lastVideoUriString != null) { Uri lastVideoUri = Uri.parse(lastVideoUriString); // Initialize your video player with the saved URI videoPlayer.setDataSource(this, lastVideoUri); videoPlayer.prepare(); // Seek to the last position and start playing videoPlayer.seekTo(lastPosition); videoPlayer.start(); } else { Toast.makeText(this, "No recent video found", Toast.LENGTH_SHORT).show(); } });
3. Important Considerations
- Permissions: If your video is stored in external storage, make sure you’ve requested the
READ_EXTERNAL_STORAGEpermission (for Android 10 and below) or use scoped storage (for Android 11+). - File Existence Check: Add a check to ensure the saved video file still exists before attempting to play it—this prevents crashes if the file was deleted or moved.
- Player Cleanup: Don’t forget to release your video player resources when the app is destroyed to avoid memory leaks.
That should do it! With these steps, tapping the FAB will instantly resume the last video your user was watching. Let me know if you run into any specific issues with your player library (e.g., ExoPlayer, MediaPlayer)—I can adjust the code accordingly.
内容的提问来源于stack exchange,提问作者Ashok

