Android - 如何使用uri源在VideoView上设置缩略图?

时间:2015-06-24 22:41:56

标签: android android-videoview

我需要在加载时从uri获取图像到我的VideoView中。当游戏被击中时,它应该消失并开始播放视频。这是我的VideoView代码 -

<RelativeLayout
    android:id="@+id/video_frame"
    android:layout_width="fill_parent"
    android:layout_height="300dp"
    android:background="#000"
    >

    <VideoView
        android:id="@+id/video"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        />

    <ImageButton
        android:id="@+id/play_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/ic_media_play"
        />

</RelativeLayout>

这就是我使用它的方式 -

final VideoView video = (VideoView) convertView.findViewById(R.id.video);

    Uri uri = Uri.parse(submission.getUrl());
    video.setVideoURI(uri);

    final ImageButton playButton = (ImageButton) convertView.findViewById(R.id.play_button);
    video.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            playButton.setVisibility(View.VISIBLE);
        }
    });
    playButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            if (video.isPlaying()){
            }else{
                video.start();
                playButton.setVisibility(View.GONE);
            }
        }

    });

3 个答案:

答案 0 :(得分:1)

VideoView显示视频。它不显示图像。同样,ImageView显示图像。它没有显示视频。

因此,请使用围绕FrameLayoutImageView的{​​{1}}。让VideoView可见,ImageView一开始就不可见。将图像加载到VideoView。当用户点击您的播放按钮时,请切换可见性,以便ImageView现在可见且VideoView不可见,并开始播放您的视频。

答案 1 :(得分:1)

创建视频的重复内容并在ImageView上显示。如果您需要显示播放按钮,则需要另一个ImageView(带有播放按钮图标)。点击Play图标图像视图在VideoView上播放视频。

    imageView.setImageBitmap(ThumbnailUtils.createVideoThumbnail(urlPath, 
    MediaStore.Video.Thumbnails.MICRO_KIND));

答案 2 :(得分:0)

我已经为缩略图创建了ImageView

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:clickable="true"
    android:focusable="true">

 <VideoView
    android:id="@+id/video_view"
    android:layout_width="match_parent"
    android:layout_height="220dp"/>
<ImageView
    android:id="@+id/videoView_thumbnail"
    android:layout_width="match_parent"
    android:layout_height="220dp"
    android:scaleType="centerCrop"/>

</RelativeLayout>

并以这种方式在Java中添加setOnPreparedListenersetOnCompletionListener

  VideoView videoView = (VideoView) view.findViewById(R.id.video_view);
  ImageView thumbnailView=(ImageView)view.findViewById(R.id.videoView_thumbnail);

  Glide.with(getApplicationContext()).load(your_Image_Path).into(thumbnailView);
 //you can add progress dialog here until video is start playing;

    mediaController= new MediaController(getContext());
    mediaController.setAnchorView(videoView);
    videoView.setMediaController(mediaController);
    videoView.setKeepScreenOn(true);
    videoView.setVideoPath(your_video_path);
    videoView.start();         //call this method for auto playing video

    videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mediaPlayer) {
            thumbnailView.setVisibility(View.GONE);
 //you can Hide progress dialog here when video is start playing;

        }
    });
    videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mediaPlayer) {
            videoView.stopPlayback();
            thumbnailView.setVisibility(View.VISIBLE);

        }
    });

它对我很好。我希望它对所有人有用。