0

I'm trying to display a video thumbnail of the post. The user is able to enter a link from Youtube, Mixer, and Twitch videos and I'm a noob so I'm not sure how to get an actual thumbnail from that videos.

Here is the code that I'm using right now, it's working well, but I don't want all videos to show exactly the same thumbnails so I'd like to show an actual thumbnail.

when (post.type){
            "twitch" -> {
                post_image.setImageResource(R.drawable.twitch)
            }
            "youtube" -> {
                post_image.setImageResource(R.drawable.youtube)
            }
            "mixer" -> {
                post_image.setImageResource(R.drawable.mixer)
            }

            else -> {
                post_image.setImageResource(R.drawable.image_placeholder)
            }
        }
Renato Lulic
  • 200
  • 2
  • 18

2 Answers2

4

As you've mentioned,

I don't want all videos to show exactly the same thumbnails so I'd like to show an actual thumbnail.

You need to use the video ID to fetch the thumbnail of the video like the following:

String url = "https://www.youtube.com/watch?v=en7IK3iH3wI"
String videoId = url.split("v=")[1]; //for this, the extracted id is "en7IK3iH3wI"

String tempThumbnailDefault = "http://img.youtube.com/vi/"+videoId+"/default.jpg" //default quality thumbnail
String tempThumbnailStandard = "http://img.youtube.com/vi/"+videoId+"/sddefault.jpg" 
//standard thumbnail
String tempThumbnailInMaxRes = "http://img.youtube.com/vi/"+videoId+"/maxresdefault.jpg" 
//maximum resolution thumbnail
String tempThumbnailInMQ = "http://img.youtube.com/vi/"+videoId+"/mqdefault.jpg" //medium quality thumbnail
String tempThumbnailInHQ = "http://img.youtube.com/vi/"+videoId+"/hqdefault.jpg"
//high quality thumbnail

Then you can use this path to load images with Glide or Picasso like the following:

//If using Glide
Glide.with(this)
     .load(tempThumbnailInHQ)
     .into(yourImageView);

//If using Picasso        
Picasso.with(context)
       .load(tempThumbnailInHQ)
       .into(yourImageView);

For fetching thumbnail from Twitch, you need to use Twitch API & a registered account on https://glass.twitch.tv. Please refer to this accepted answer for knowing the steps to use it.

Hope this helps!

SaadAAkash
  • 3,065
  • 3
  • 19
  • 25
1

If you have video url then you can use Glide for video thumbnail. You can check the doc here https://github.com/bumptech/glide

Nam Đỗ
  • 146
  • 5