如何在Android中捕获VideoView的屏幕截图或视频帧

cod*_*ark 3 video android android-video-player android-videoview

我尝试了此博客( http://android-er.blogspot.kr/2013/05/get-current-frame-in-videoview-using.html )中的以下教程,该教程展示了如何捕获视频帧使用来自视频源的 MediaMetadataRetriever。但是,它仅在视频位于手机本地时才有效。

有没有办法在VideoView 通过 IP 流式传输视频时捕获视频帧?

Hit*_*ahu 5

Video View 是 SurfaceView 的子类,因此 Pixel Copy 也可以截取视频 View 的屏幕截图

将此方法放在某个 Util 类中

/**
 * Pixel copy to copy SurfaceView/VideoView into BitMap
 */
 fun usePixelCopy(videoView: SurfaceView,  callback: (Bitmap?) -> Unit) {
    val bitmap: Bitmap = Bitmap.createBitmap(
        videoView.width,
        videoView.height,
        Bitmap.Config.ARGB_8888
    );
    try {
    // Create a handler thread to offload the processing of the image.
    val handlerThread = HandlerThread("PixelCopier");
    handlerThread.start();
    PixelCopy.request(
        videoView, bitmap,
        PixelCopy.OnPixelCopyFinishedListener { copyResult ->
            if (copyResult == PixelCopy.SUCCESS) {
                callback(bitmap)
            }
            handlerThread.quitSafely();
        },
        Handler(handlerThread.looper)
    )
    } catch (e: IllegalArgumentException) {
        callback(null)
        // PixelCopy may throw IllegalArgumentException, make sure to handle it
        e.printStackTrace()
    }
}
Run Code Online (Sandbox Code Playgroud)

用法

usePixelCopy(videoView) { bitmap: Bitmap? ->
            processBitMp(bitmap)
        }
Run Code Online (Sandbox Code Playgroud)