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?
One Solution collect form web for “How to clear SurfaceTexture after using it with a MediaPlayer?”
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.