【问题标题】:Android - how to set a thumbnail on a VideoView with a uri source?Android - 如何在带有 uri 源的 VideoView 上设置缩略图?
【发布时间】:2018-12-26 17:57:17
【问题描述】:

我需要在加载时将图像从 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);
            }
        }

    });

【问题讨论】:

    标签: android android-videoview


    【解决方案1】:

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

    所以,使用FrameLayout 包裹ImageViewVideoView。一开始就让ImageView 可见,VideoView 不可见。将图像加载到ImageView。当用户单击您的播放按钮时,切换可见性,使VideoView 现在可见,ImageView 不可见,然后开始播放您的视频。

    【讨论】:

    • 我的问题可能有点不清楚。我需要一种从视频本身中提取图像的方法。不确定这是否可行。
    • @Suchi:如果MediaStore 知道视频,它应该有一个您可以检索的缩略图,通常通过图像加载库(参见this sample app)。如果MediaStore 不知道该视频,ThumbnailUtils 应该可以提取一个,尽管我没有使用它。
    【解决方案2】:

    创建视频缩略图并将其显示在 ImageView 上。如果您需要显示播放按钮,则需要另一个 ImageView(带有播放按钮图标)。点击播放图标图像视图在 VideoView 上播放视频。

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

    【讨论】:

      【解决方案3】:

      我为这样的缩略图创建了一个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);
      
              }
          });
      

      它非常适合我。希望对大家有用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-06-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-03
        • 2013-07-16
        • 2019-12-19
        • 2021-01-01
        相关资源
        最近更新 更多