Andrew Shepherd Andrew Shepherd - 1 year ago 129
Android Question

MediaPlayer cannot render to TextureView after image render

I have a

rendering videos to a
. This is working.

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

Here's my code to render the bitmap:

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

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

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

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

I'm working on Android 4.2.2.

Answer Source

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

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.

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.)

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download