我试图使用Exoplayer在视频中的特定点捕获视频帧,但此代码总是捕获视频的第一帧。如何修复
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(yourPath);
Bitmap bitmap = retriever.getFrameAtTime(simpleExoPlayer.getCurrentPosition());
这个方法应该在特定的点得到你想要的框架
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;
}
应该是毫秒* 1000。固定!
您可以简单地使用exoplayer中存在的seekto()函数来播放视频的任何时间戳!
设置Exoplayer的代码: XML代码:
<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"/>
在你的类中定义这些变量:
private Context context;
private PlayerView videoPlayerView;
private ExoPlayer player;
String videoUri = "http://commondatastorage.googleapis.com/gtvvideos-bucket/sample/ForBiggerEscapes.mp4";
在类中创建一个函数来设置pexoplayer:
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();
}
传递给你的playerview id (XML id)并运行应用程序!:)
//从第0秒开始播放视频
videoPlayerView.setVideoPlayer(0);
//从第10秒开始播放
videoPlayerView.setVideoPlayer(10);