Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the preview image in videoview before playing

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

  1. Video should not play when the activity gets open.
  2. It shoud display the starting video image(currently its displaying black color)
  3. It should play only when the user click on the video.
    please help me.
like image 274
Vignesh Avatar asked Jun 13 '13 05:06

Vignesh


People also ask

What is the use of video view?

In Android, VideoView is used to display a video file.


2 Answers

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:

VideoView mVideoView = (VideoView) findViewById( R.id.video_preview );  mVideoView.setVideoURI( yourVideoPath );  mVideoView.seekTo( 1 );                 // 1 millisecond (0.001 s) into the clip. 

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.

like image 137
Joshua Pinter Avatar answered Sep 25 '22 16:09

Joshua Pinter


Create video thumbnail using this

Bitmap thumb = ThumbnailUtils.createVideoThumbnail("file path/url",                             MediaStore.Images.Thumbnails.MINI_KIND); 

and set to videoview

BitmapDrawable bitmapDrawable = new BitmapDrawable(thumb);             mVideoView.setBackgroundDrawable(bitmapDrawable); 
like image 20
Shijil Avatar answered Sep 24 '22 16:09

Shijil