图像渲染后 MediaPlayer 无法渲染到 TextureView [英] MediaPlayer cannot render to TextureView after image render

查看:43
本文介绍了图像渲染后 MediaPlayer 无法渲染到 TextureView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 MediaPlayer 将视频渲染到 TextureView.这是有效的.

I have a MediaPlayer rendering videos to a TextureView. This is working.

现在,我想在此 TextureView 上显示给定时间的静止图像,然后让 MediaPlayer 将视频渲染到相同的 TextureView.

Now, I want to display a still image on this TextureView for a given time, then get the MediaPlayer to render a video to the same TextureView.

这是我渲染位图的代码:

Here's my code to render the bitmap:

Canvas canvas = mTextureView.lockCanvas();
canvas.drawBitmap(sourceBitmap, matrix, new Paint());
mTextureView.unlockCanvasAndPost(canvas);

此后,任何播放视频的尝试都会导致视频播放器触发 ERROR_INVALID_OPERATION (-38).

After this, any attempts to play videos result in ERROR_INVALID_OPERATION (-38) being triggered from the video player.

我尝试注释掉对 drawBitmap 的调用,但错误仍然发生.似乎调用 lockCanvas 后跟 unlockCanvasAndPost 的简单行为导致 TextureView 不适合 MediaPlayer使用.

I tried commenting out the call to drawBitmap, and the error still happened. It seems that the simple act of calling lockCanvas followed by unlockCanvasAndPost results in the TextureView being unsuitable for the MediaPlayer to use.

有什么方法可以将 TextureView 重置为允许 MediaPlayer 使用它的状态?

Is there some way that I can reset the TextureView to a state that allows the MediaPlayer to use it?

我正在开发 Android 4.2.2.

I'm working on Android 4.2.2.

推荐答案

由于 Android 应用框架的限制(至少从 Android 4.4 开始),您不能这样做.

You can't do this, due to a limitation of the Android app framework (as of Android 4.4 at least).

作为 TextureView 基础的 SurfaceTexture 是一个缓冲区消费者.MediaPlayer 是缓冲区生产者的一个例子,Canvas 是另一个例子.附加生产者后,您必须先将其分离,然后才能附加第二个生产者.

The SurfaceTexture that underlies the TextureView is a buffer consumer. The MediaPlayer is one example of a buffer producer, Canvas is another. Once you attach a producer, you have to detach it before you can attach a second producer.

问题在于无法分离基于软件(Canvas)的缓冲区生产者.可能有,但没有.所以一旦你用 Canvas 画画,你就卡住了.(在此处有说明.)

The trouble is that there is no way to detach a software-based (Canvas) buffer producer. There could be, but isn't. So once you draw with Canvas, you're stuck. (There's a note to that effect here.)

可以分离一个 GLES 生产者.例如,在 Grafika 的视频播放器类之一中,您可以找到 clearSurface() 使用 GLES 将表面清除为黑色的方法.请注意,EGL 上下文和窗口是在该方法的范围内创建和显式释放的.您可以扩展该方法以显示图像.

You can detach a GLES producer. For example, in one of Grafika's video player classes you can find a clearSurface() method that clears the surface to black using GLES. Note the EGL context and window are created and explicitly released within the scope of the method. You could expand the method to show an image instead.

这篇关于图像渲染后 MediaPlayer 无法渲染到 TextureView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆