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?

  • Is it possible to register a receiver in a test case?
  • How to generate a ListView with headers above some sections?
  • Android custom widget styles: how to put them into a namespace?
  • Android Studio IDE on Mac os:error=13, Permission denied
  • 'Connection to the server was unsuccessful' error when launching phonegap app on android emulator
  • Adding strings localization files from a server
  • Related posts:

    How to install apk files on my android emulator with Eclipse?
    What does getLine1Number() return with dual SIM phones?
    JavaPoet + Android Studio “addModifiers(Modifier) cannot be applied to Modifier”
    Android: How to fill RelativeLayout to full width of screen?
    Android RecyclerView which is in a Fragment which is in a ViewPager does not reload/refresh views if...
    Camera in Android
  • SimpleCursorAdapter alternative
  • RecyclerView - Where should I handle its click events?
  • Image View not Wrapping Contents
  • actionbar search will not close after search
  • Registration ID become “NotRegistered” for no reason
  • Activity with transparent background
  • 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.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.