(Android/Java) How to capture a frame from the VideoPlayer widget flutter?

99封情书 提交于 2019-12-11 16:26:53

问题


Is there any way to get the current frame from the VideoPlayer in the form of a Image?

(Note: The RenderRepaintBoundary technique doesn't work in this case)

Update:

I found a plugin which can solve my problem in its own way. https://pub.dev/packages/video_thumbnail#-readme-tab-

But the problem is that it only captures the first frame irrespective of the time given to it (in ms).

After navigating and testing different parts of the plugin for a while this is where I had ended up.

Can any developer tell me the reason for always getting the first frame irrespective of the value of timeMs?

Main plugin code:

    public static Bitmap createVideoThumbnail(String video, int targetSize, int timeMs) {
        Bitmap bitmap = null;
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        try {
            Log.d(TAG, String.format("setDataSource: %s )", video));
            if (video.startsWith("file://") || video.startsWith("/")) {
                retriever.setDataSource(video);
            } else {
                retriever.setDataSource(video, new HashMap<String, String>());
            }

            if (targetSize != 0) {
                if (android.os.Build.VERSION.SDK_INT >= 27) {
                    // API Level 27
                    bitmap = retriever.getScaledFrameAtTime(timeMs * 1000, 0, targetSize, targetSize);
                } else {
                    bitmap = retriever.getFrameAtTime(timeMs * 1000);
                    if (bitmap != null) {
                        int width = bitmap.getWidth();
                        int height = bitmap.getHeight();
                        int max = Math.max(width, height);
                        float scale = (float) targetSize / max;
                        int w = Math.round(scale * width);
                        int h = Math.round(scale * height);
                        Log.d(TAG, String.format("original w:%d, h:%d, scale:%6.4f => %d, %d", width, height, scale, w,
                                h));
                        bitmap = Bitmap.createScaledBitmap(bitmap, w, h, true);
                    }
                }
            } else {
                bitmap = retriever.getFrameAtTime(timeMs * 1000);
            }
        } catch (IllegalArgumentException ex) {
            ex.printStackTrace();
        } catch (RuntimeException ex) {
            ex.printStackTrace();
        } finally {
            try {
                retriever.release();
            } catch (RuntimeException ex) {
                ex.printStackTrace();
            }
        }

        return bitmap;
    }

If anyone feels that the above method is working as it should work then please don't hesitate to ask for more details.

Note: The timeMs parameter always gets the right time in ms.

来源:https://stackoverflow.com/questions/58608062/android-java-how-to-capture-a-frame-from-the-videoplayer-widget-flutter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!