问题
I have a ListView
and I want to populate it with video form a URL. I can show Pictures in ListView adapter easily from URL, but how to load a video thumbnail?
回答1:
You can use MediaMetadataRetriever to get your video thumbnail.
new AsyncTask<Void, Void, Bitmap>() {
@Override
protected Bitmap doInBackground(Void... params) {
Bitmap bitmap = null;
String videoPath = "http://techslides.com/demos/sample-videos/small.mp4";
MediaMetadataRetriever mediaMetadataRetriever = null;
try {
mediaMetadataRetriever = new MediaMetadataRetriever();
if (Build.VERSION.SDK_INT >= 14)
// no headers included
mediaMetadataRetriever.setDataSource(videoPath, new HashMap<String, String>());
else
mediaMetadataRetriever.setDataSource(videoPath);
// mediaMetadataRetriever.setDataSource(videoPath);
bitmap = mediaMetadataRetriever.getFrameAtTime();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (mediaMetadataRetriever != null)
mediaMetadataRetriever.release();
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
if (bitmap != null)
((ImageView) findViewById(R.id.your_image_id)).setImageBitmap(bitmap);
}
}.execute();
来源:https://stackoverflow.com/questions/41956167/how-to-load-video-thumbnail-from-video-url-in-listview-adapter