Only loading video into VideoView without instantly playing
I was also thinking about showing a thunbnail as sghael stated. But I think this might result in reduced performance.
So what I'm doing now is just call
videoView.seekTo(1);
to forward the video to the first millisecond. This works fine for me and is fast to implement.
I also wanted my VideoView to be in a paused state when the activity started. I could not find a simple way to have the VideoView show the first (non-black) frame of the video.
As a workaround, I created a bitmap thumbnail from the video resource, and then place that thumbnail as the background to the VideoView on activity start. Then when the Video begins playing, you only need to null out the background (or else your playing video is hidden behind your background image).
Bitmap thumbAsBitmap = null;
BitmapDrawable thumbAsDrawable = null;
private String current;
private void setupVideo() {
try {
setVideoFilePath();
if (current == null || current.length() == 0) {
Toast.makeText(PreviewMessage.this, "File URL/path is empty",
Toast.LENGTH_LONG).show();
} else {
keepScreenOn();
mVideoView.setVideoPath(getDataSource(current));
mVideoView.setOnCompletionListener(this);
// create and place a thumbnail for the start state
thumbAsBitmap = ThumbnailUtils.createVideoThumbnail(current, MediaStore.Images.Thumbnails.MINI_KIND);
thumbAsDrawable = new BitmapDrawable(thumbAsBitmap);
mVideoView.setBackgroundDrawable(thumbAsDrawable);
mVideoView.pause();
isPlaying = false;
}
} catch (Exception e) {
if (mVideoView != null) {
mVideoView.stopPlayback();
}
}
}
Then whereever your playbutton is, you can do something like
mPlay.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if(!isPlaying){
keepScreenOn();
mPlay.setText("Pause");
// make sure to null the background so you can see your video play
mVideoView.setBackgroundDrawable(null);
mVideoView.start();
isPlaying = true;
} else {
mPlay.setText("Play");
mVideoView.pause();
isPlaying = false;
}
}
});
VideoView
will not start playback automatically, at least if you have a MediaController
attached. You need to call start()
to have it start playback. Hence, if you do not want it to start playback, do not call start()
.