【问题标题】:I want to create thumbnail from video url of server in android我想从 android 中服务器的视频 url 创建缩略图
【发布时间】:2017-03-03 14:03:46
【问题描述】:

我的代码,

public static Bitmap retriveVideoFrameFromVideo(String videoPath) throws Throwable {

    Bitmap bitmap = null;
    MediaMetadataRetriever mediaMetadataRetriever = null;
    try {
        mediaMetadataRetriever = new MediaMetadataRetriever();
        if (Build.VERSION.SDK_INT >= 14)
            mediaMetadataRetriever.setDataSource(videoPath, new HashMap<String, String>());
        else
            mediaMetadataRetriever.setDataSource(videoPath);
        //   mediaMetadataRetriever.setDataSource(videoPath);
        bitmap = mediaMetadataRetriever.getFrameAtTime();
    } catch (Exception e) {
        e.printStackTrace();
        throw new Throwable(
                "Exception in retriveVideoFrameFromVideo(String videoPath)"
                        + e.getMessage());

    } finally {
        if (mediaMetadataRetriever != null) {
            mediaMetadataRetriever.release();
        }
    }
    return bitmap;
}

这是创建缩略图,但我在 ListView 使用它时花了很多时间,然后 ListView 挂断了。

【问题讨论】:

标签: android mediametadataretriever


【解决方案1】:

如果你使用的是RecycleView,你需要在异步方法中运行这个任务就像这样在onBindViewHolder()中,如果你使用的是ListView,你需要使用getView()

 new AsyncTask<String, String, String>() {
            Bitmap bitmapVideo;

            @Override
            protected String doInBackground(String... strings) {
                try {
                   //Your method call here
                    bitmapVideo =retriveVideoFrameFromVideo(strings[0]);
                } catch (Throwable throwable) {
                    throwable.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(String id) {
                super.onPostExecute(id);
                if (bitmapVideo != null) {
                  //Load your bitmap here
                    holder.imgVideoThumb.setImageBitmap(bitmapVideo);
                }
            }
        }.execute(getYourVideolink());

为了提高效率,您将位图图像保存在本地,然后在调用 AsyncTask() 之前检查天气,如果该图像比从本地加载并且没有新的可以再次运行 AsyncTask(),则该图像已经保存在本地

【讨论】:

  • 谢谢兄弟。但是当我滚动列表视图然后在视频上设置所有其他缩略图时也会出现一个问题
  • 我没找到你??
  • 您必须使用Viewholder 管理并将图像保存在本地并从本地加载
  • 当我滚动列表视图然后缩略图设置但视频不匹配时,我的列表视图有多个视频 这是因为我们在 asyncTask 中设置了图像视图
  • 您最好使用RecycleView 以获得更高的效率,因为'ListView' 没有被弃用..它在recycleview 中可以正常工作
猜你喜欢
  • 1970-01-01
  • 2018-05-15
  • 2018-04-18
  • 2017-12-10
  • 2011-04-17
  • 2015-03-30
  • 2011-11-22
  • 2010-10-01
  • 2020-11-29
相关资源
最近更新 更多