Kotlin-显示现有youtube、twitch、混音视频和直播流的视频缩略图



我正在尝试显示帖子的视频缩略图。用户可以从Youtube、Mixer和Twitch视频中输入链接,而我是一个傻瓜,所以我不知道如何从这些视频中获得实际的缩略图。

这是我现在使用的代码,它运行得很好,但我不希望所有视频都显示完全相同的缩略图,所以我想显示一个实际的缩略图。

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)
}
}

正如您所提到的,

我不希望所有视频都显示完全相同的缩略图,所以我想显示一个实际的缩略图。

您需要使用视频ID来获取视频的缩略图,如下所示:

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

然后你可以使用这个路径加载Glide或Picasso的图像,如下所示:

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

要从Twitch获取缩略图,您需要使用Twitch API&上的注册帐户https://glass.twitch.tv.请参阅这个公认的答案,了解使用它的步骤。

希望这能有所帮助!

如果你有视频url,那么你可以使用Glide来获取视频缩略图。你可以在这里查看文档https://github.com/bumptech/glide

最新更新