Reputation: 71
I'm trying to capture video frames at specific points in a video using Exoplayer but this code always capture the first frame of the video. How do I fix it
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(yourPath);
Bitmap bitmap = retriever.getFrameAtTime(simpleExoPlayer.getCurrentPosition());
Upvotes: 0
Views: 2977
Reputation: 19
You can simply use seekto() function present in exoplayer to play video from any timestamp of video!
code to setup Exoplayer:
XML code:
<com.google.android.exoplayer2.ui.PlayerView
android:id="@+id/videoPlayerView"
android:layout_width="0dp"
android:layout_height="200dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginTop="10dp"/>
define these variables in you class:
private Context context;
private PlayerView videoPlayerView;
private ExoPlayer player;
String videoUri = "http://commondatastorage.googleapis.com/gtvvideos-bucket/sample/ForBiggerEscapes.mp4";
Creating a function in your class to setupExoPlayer:
public void setVideoPlayer(long time) {
player = new ExoPlayer.Builder(context).build();
//Bind the player to the view.
playerView.setPlayer(player);
MediaItem mediaItem = MediaItem.fromUri(videoUri);
// Set the media item to be played.
player.setMediaItem(mediaItem);
// SeekTo particular timestamp in videoPlayer
player.seekTo(time * 1000);
// Prepare the player.
player.prepare();
// Start the playback.
player.play();
}
pass to your playerview id (XML id) and run the app! :)
// to start video from 0th second
videoPlayerView.setVideoPlayer(0);
// to start video from 10th second
videoPlayerView.setVideoPlayer(10);
Upvotes: 1
Reputation: 176
This method should get you the frame you want at the specific point
public static Bitmap getVideoFrame(Context context, Uri uri, long time) {
Bitmap bitmap = null;
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
try {
retriever.setDataSource(context, uri);
bitmap = retriever.getFrameAtTime(time);
} catch (RuntimeException ex) {
ex.printStackTrace();
} finally {
try {
retriever.release();
} catch (RuntimeException ex) {
ex.printStackTrace();
}
}
return bitmap;
}
Upvotes: 2