I have been looking for a way to get rid of the nasty black initial screen on a VideoView before the start() method is run.
I have tried with background image on the
It's a little late for this answer, but maybe other users have the same problem and find this question..
I have dealt with it, by setting a BackgroundResource initially and then, when starting the video, i have set the background to an invisible color..
VideoView myView = findViewById(R.id.my_view);
myView.setBackgroundResource(R.drawable.some_resource);
// some stuff
// this is when starting the video
myView.setVideoUri(someUri);
// also set MediaController somewhere...
//...
// now set the backgroundcolor to be not visible (first val of Color.argb(..) is the alpha)
myView.setBackGroundColor(Color.argb(0, 0, 0, 0));
//...
myView.start();
For me setting the setZOrderOnTop
did not completely remove the initial black frame while playing an mp4 video. It, however, did reduce the time for which the black frame appears. I wanted to remove the initial black frame completely, so I played around and found that seeking the video forward by 100ms did the trick for me.
As a note, I am using the video in a loop, so if you do not want to loop the video just remove
mp.isLooping = true
Following is the snippet which I used to fix the issue:
val path = "android.resource://" + packageName + "/" + R.raw.my_video
videoView.setVideoURI(Uri.parse(path))
videoView.setZOrderOnTop(true)
videoView.seekTo(100)
videoView.start()
videoView.setOnPreparedListener { mp ->
videoView.setZOrderOnTop(false)
mp.isLooping = true // Loops the video
}
It would still be great if I get an exact explanation of why the above worked if someone finds it helpful.
None of the Above worked for me.
In my case, onPrepared
gets called BEFORE the black frame went away, so I would still see the black frame.
I needed a solution where the video appeared shortly after the first frame.
So what I did was set the VideoView alpha to 0 in xml:
android:alpha="0"
and then before I start the video I animate the alpha back to 1:
videoView.animate().alpha(1);
videoView.seekTo(0);
videoView.start();
alternatively, you can just post a delayed Runnable to set the alpha to 1, instead of animating it.
For people still looking for answer for this, calling VideoView.start()
and VideoView.pause()
in succession inside onPrepared
worked for me. I know this may not be the ideal way of achieving this however it might be the one with minimal workaround required in the code. Hope this works for you too.
@Override
public void onPrepared(MediaPlayer mp) {
mVideoView.start();
mVideoView.pause();
}