I created an VideoView
in my activity, below is the code.
VideoView vvVideos = (VideoView) rootView.findViewById(R.id.videoView);
MediaController mediacontroller = new MediaController(ctx);
mediacontroller.setAnchorView(vvVideos);
Uri video = Uri.parse("android.resource://" + packageName +"/"+R.raw.sample);
vvVideos.setMediaController(mediacontroller);
LayoutParams params=vvVideos.getLayoutParams();
params.height=150;
vvVideos.setLayoutParams(params);
vvVideos.setVideoURI(video);
vvVideos.requestFocus();
vvVideos.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
vvVideos.start();
}
});
Now the video gets started to play when the activity gets created. I want to make my activity as follows
- Video should not play when the activity gets open.
- It shoud display the starting video image(currently its displaying black color)
- It should play only when the user click on the video.
please help me.
Thought I'd share my solution. The
seekTo
method works great but only for some devices. Here is my work around. I handle this in the onPrepared method for the onPreparedListener but its up to you.Now when you play the video you will need to remove this background image like so:
Enjoy!
I know it's an old question, but I needed the same solution and couldn't find the answer anywhere else so I did the solution and I'm sharing the love here:
I just created a little class for it:
it simply asks to start playing and pauses the video back on the first frame as soon as it's actually playing (meaning it loaded the file and it's already rendering it on the SurfaceView).
The important note here is to remember to call
stop()
on this class so it can properly clean up and not memory leak anything.I hope it helps.
Create video thumbnail using this
and set to videoview
You can do it with
Glide 4.x
. It will fetch the first frame of your video show it in anImageView
add to your
build.gradle
and in your Class
Use
seekTo( 1 )
to show the first frame.Ensure the movie is paused and then use
seekTo()
to show the first frame of the video:NOTE: We use
.seekTo( 1 )
because setting.seekTo( 0 )
did not work on Android 9.To have it play when clicked on has been answered by @Lingviston in another answer.
You can use a @Joshua Pinter's answer to solve the problem. But I want to give you more suggestion about it. You yourself answer that
seekTo(100)
works instead ofseekTo(1)
. Neither of the two ways is perfect. That is becauseseekTo(1)
would get a black image and theseekTo(100)
may got an exception.I prefer to do like this:
If you don't have the backend server, it may be better. Or, if you had a backend server, you can try it in this way.
The thumbnail is calculate by the server, the client just display the image which the backend response. And when it come to server side, you can do a lot of things.
If you want to discuss more about the two questions we can discuss them later.