NotesUESTC/Android开发笔记/Android获取视频封面.md

28 lines
815 B
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

```java
// 给定视频的URL
final String url = videoUrl;
// 调用Android的MediaMetadataRetriever获取视频封面
final MediaMetadataRetriever mmr = new MediaMetadataRetriever();
new Thread(() -> {
Map<String, String> headers = new HashMap<>() ;
// 指定header和url
mmr.setDataSource(url, headers);
// 设置截取视频第10秒钟内容
Long sec = 10L;
// sec转us单位
Long msec = sec * 1000L;
Long us = msec * 1000L;
final Bitmap image = mmr.getFrameAtTime(us, MediaMetadataRetriever.OPTION_CLOSEST_SYNC);
// 更新imageview显示图片
runOnUiThread(() -> {
ImageView tv = findViewById(R.id.imageView);
tv.setImageBitmap(image);
});
// 释放mmr断开连接
mmr.release();
}).start();
```
HTTP视频请求Range的计算方法