how to clear surface holder when media player is finished?

beginners picture beginners · Aug 10, 2012 · Viewed 10.4k times · Source

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~~!

Answer

user2743991 picture user2743991 · Sep 20, 2013

Took me two weeks to figure this out. By setting the surfaceholder to TRANSPARENT, Android will destroy the surface. Then setting it back to OPAQUE creates a new surface "clearing" the surface. Note surfacecreate and surfacedestroy events will fire, so if you have code there, beware. I put a imageview set to black to give it a black background. There maybe better ways for that.

private void playVideoA() { 
     imageViewBlack.bringToFront();
     surfaceHolder.setFormat(PixelFormat.TRANSPARENT);
     surfaceHolder.setFormat(PixelFormat.OPAQUE);
     surfaceView.bringToFront();
     mediaPlayerA.setDisplay(surfaceHolder);
     //surfaceView.setAlpha((float) 0.01);
     mediaPlayerA.start();
};
private void prepareVideoA(String url) {
     try {
        mediaPlayerA = new MediaPlayer();
        mediaPlayerA.setDataSource(url);
        mediaPlayerA.prepareAsync();
        mediaPlayerA.setOnPreparedListener(this);
        mediaPlayerA.setOnCompletionListener(this);
        mediaPlayerA.setOnBufferingUpdateListener(this);
        mediaPlayerA.setOnInfoListener(this);
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SecurityException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
};
@Override
public void onPrepared(MediaPlayer mp) {
     playVideoA()
}