Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear SurfaceTexture after using it with a MediaPlayer?

I'm using a ListView with TextureView children. The TextureView uses a MediaPlayer to play videos. When the TextureView gets recycled, the last frame remains on the surface until the next MediaPlayer makes use of it.

What is the easiest way to "clear" the TextureView's surface (e.g black) so the old frames do not appear?

like image 492
Gilbert Avatar asked Oct 14 '14 07:10

Gilbert


2 Answers

After struggling with this for a while, I've come up with a couple of possible solutions:

a) clear the surface using GLES. You can see an example in Grafika (check out the clearSurface() method). I'm personally not a fan of this because I don't think that a jarring transition to black looks particularly good, and it's a fair bit of technical overhead if you aren't already using GL code. Additionally, this only seems to work for API 17+.

b) My chosen solution was to simply create a new TextureView and add it to the appropriate view every time I wanted to play a video, instead of reusing an old one. I would hesitate to call this a good solution, but it's the least horrible one I could find.

c) One thing you might be able to do if you're playing the same video again is to try to seek to the first frame of the video when it's done playing. Then the next time the last frame flashes, it won't be visible since it will be the same frame as the beginning of the video. I had some success with this but couldn't get it to work reliably on all devices. It's also pretty clearly a hack.

Good luck, this was immensely frustrating to try to fix.

like image 68
gcgrant Avatar answered Oct 12 '22 08:10

gcgrant


2020 simplest way is to simply set the alpha of the texture view to 0.

mTextureView.setAlpha(0);

like image 41
Wharbio Avatar answered Oct 12 '22 06:10

Wharbio