how to clear surface holder when media player is finished?

android, surfaceview

Solution

The `SurfaceHolder` has `lockCanvas` methods that will allow you to draw directly to the `Surface`. Use the `drawColor` method of the `Canvas` to fill it with black.

That said, it may be preferable to remove the `SurfaceView` (as suggested by smile2you), because that should destroy the `Surface` altogether and free up unused resources. And definitely make sure you are calling `release` on the `MediaPlayer`s after they are done with playback. Holding on to too many video resources will crash your app.

Problem

I made a video player with surfaceview and mediaplayer. i have 10 videos and 10 buttons. if click on each buttons, each videos are playing. here is my code.. ``` //onCreate holder = surfaceview.getHolder(); holder.addCallback(this); holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); //Button1 if(mp == null)mp = new MediaPlayer(); mp.setDataSource(mediaplay_path); mp.setDisplay(holder); mp.setScreenOnWhilePlaying(true); mp.setAudioStreamType(AudioManager.STREAM_MUSIC); mp.prepare(); mp.start(); //Button2 if(mp != null){ mp.stop(); mp.reset(); } mp.setDataSource(mediaplay_path2); mp.setDisplay(holder); mp.setScreenOnWhilePlaying(true); mp.setAudioStreamType(AudioManager.STREAM_MUSIC); mp.prepare(); mp.start(); //Button3~Button10 is same as Button2.. ``` everything is fine. my custom videoview is working alright. but when the video turns to the next, the last scene of the previous video is remain for a while and turns to the next video scene. i think it's because the previous surfaceview should be clear before next video is playing. but i have no idea how to clear the surfaceview or surface holder. i've searched for this but only could find how to play the video, not how to clear the surfaceview which is set the disaply from mediaplayer. please help me~~!

Original source