2

I have a video player that gets reset and loaded with a new video in one activity. One of the problems I had with this approach was that the last frame of the previous window was still shown when the next video is loaded. Several people have the same issue.

I solved this by doing the following in the onCompletion listener, similar to what is explained in this question and this other one:

mPlayer.stop();
mPlayer.release();
Canvas canvas = mPlayView.getHolder().lockCanvas();
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
mPlayView.getHolder().unlockCanvasAndPost(canvas);
mPlayer = null;

Then later, I call this again:

mPlayer = new MediaPlayer();
mPlayer.setDataSource(videoPath);
mPlayer.setDisplay(mSurfaceHolder);
mPlayer.setScreenOnWhilePlaying(true);
mPlayer.setOnPreparedListener(this);
mPlayer.setOnCompletionListener(this);
mPlayer.setOnVideoSizeChangedListener(this);
mPlayer.setOnErrorListener(this);
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer.prepare();
// wait for onPrepared callback and play

The problem is that the video play surface is still black—no video is shown anymore.

How do I "reset" the canvas and make it show the video again?

Or if that does not work, how do I clear the canvas of the last frame instead? I tried this but the next loaded video still shows the last frame of the previous one.

Community
  • 1
  • 1
slhck
  • 36,575
  • 28
  • 148
  • 201
  • The answer to http://stackoverflow.com/questions/25660994/clear-video-frame-from-surfaceview-on-video-complete explains why drawing on it with Canvas prevents the surface from being re-used, and has a pointer to a solution with OpenGL ES that should work. (It works with MediaCodec, haven't tried it with MediaPlayer.) Another solution is to create a new opaque black View on top of the player, and show/hide it. – fadden Nov 17 '14 at 16:27

0 Answers0