1
0
forked from uestc/Notes
Notes/Android开发笔记/Android获取视频封面.md
2024-10-17 15:36:48 +08:00

28 lines
815 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

```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的计算方法