VideoView getDrawingCache 返回黑色

发布于 2024-10-21 20:00:27 字数 518 浏览 2 评论 0原文

所以我正在尝试制作 VideoView 的屏幕截图。我认为最简单的方法是:

videoView.setDrawingCacheEnabled(true);

然后当我需要截图时:

Bitmap screenshot = videoView.getDrawingCache();

但由于某种原因,我每次得到的位图都是黑色的。有人在这方面取得过成功吗?我也尝试过:

Bitmap bitmap = Bitmap.createBitmap(videoView.getWidth(), videoView.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
videoView.draw(canvas);

但是,这又给我返回了黑色图像。我可以看到 Android javadoc 中几乎没有记录 VideoView。有什么帮助吗?

So I'm trying to do a screen shot of a VideoView. I figured the easiest way would be:

videoView.setDrawingCacheEnabled(true);

Then when I need to take a screenshot:

Bitmap screenshot = videoView.getDrawingCache();

But for some reason the bitmap I get back is just black every time. Anyone had any success with this? I also tried:

Bitmap bitmap = Bitmap.createBitmap(videoView.getWidth(), videoView.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
videoView.draw(canvas);

But once again, this returns me a black image. I can see that the VideoView is hardly even documented in the Android javadocs. Any help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晨曦慕雪 2024-10-28 20:00:27

来自 View#setDrawingCacheEnabled 的文档:

启用绘图缓存类似
硬件设置层
加速已关闭。什么时候
硬件加速已开启,
启用绘图缓存没有
因为系统对渲染的影响
使用不同的机制
忽略标志的加速度。
如果您想使用位图
视图,即使硬件加速
已启用,请参阅 setLayerType(int,
android.graphics.Paint) 为
有关如何启用软件的信息
和硬件层。

VideoView 可能通过硬件加速运行并绕过缓存机制。良好的源头潜水可能会带来一些启发。

编辑 - 此帖子似乎澄清了一些事情:

抱歉,就其本质而言,SurfaceView 确实如此
不在普通视图层次结构中绘制
更新系统,这样就不会被拉进来了
那个。

(VideoView是SurfaceView的子级)

From the docs for View#setDrawingCacheEnabled:

Enabling the drawing cache is similar
to setting a layer when hardware
acceleration is turned off. When
hardware acceleration is turned on,
enabling the drawing cache has no
effect on rendering because the system
uses a different mechanism for
acceleration which ignores the flag.
If you want to use a Bitmap for the
view, even when hardware acceleration
is enabled, see setLayerType(int,
android.graphics.Paint) for
information on how to enable software
and hardware layers.

It's possible that the VideoView operates with hardware acceleration and is bypassing the caching mechanism. A good source dive might shed some light.

Edit - this post seems to clear some things up:

Sorry, by its nature a SurfaceView does
not draw in the normal view hierarchy
update system, so it won't be drawn in
that.

(VideoView is a child of SurfaceView)

冷夜 2024-10-28 20:00:27

从 API 10 开始,您可以使用 MediaMetadataRetriever 在给定时间检索帧(以微秒为单位)。这是示例代码:

public Bitmap videoFrame(String uri, long msec) {       
    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    try {                       
        retriever.setDataSource(uri);            
        return retriever.getFrameAtTime(msec);
    } catch (IllegalArgumentException ex) {
        ex.printStackTrace();
    } catch (RuntimeException ex) {
        ex.printStackTrace();
    } finally {
        try {
            retriever.release();
        } catch (RuntimeException ex) {
        }
    }
    return null;
}

Since API 10, you can use MediaMetadataRetriever to retrieve a frame at given time (in micro seconds). Here is a sample code:

public Bitmap videoFrame(String uri, long msec) {       
    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    try {                       
        retriever.setDataSource(uri);            
        return retriever.getFrameAtTime(msec);
    } catch (IllegalArgumentException ex) {
        ex.printStackTrace();
    } catch (RuntimeException ex) {
        ex.printStackTrace();
    } finally {
        try {
            retriever.release();
        } catch (RuntimeException ex) {
        }
    }
    return null;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文