我正试图使用YouTubeThumbnailView为一系列youtube视频设置多个缩略图。问题是相同的缩略图重复了一次又一次,所以我正在寻找一个如何实现这一点的例子。
我曾试图在谷歌上搜索这个问题,但我只能找到一个缩略图的例子——我只是在寻找一个如何设置多个缩略图(正确)的例子。
YouTubeThumbnail查看文档:
https://developers.google.com/youtube/android/player/reference/com/google/android/youtube/player/YouTubeThumbnailView
上一次(不成功)尝试:
YouTube API未按预期播放视频
我知道你的问题很久以前就发布了,但对于稍后会尝试找到解决方案的人来说。
YouTubeThumbnailView youTubeThumbnailView;
LinearLayout ll = new LinearLayout(this);
for(int i = 0 ; i < videos.length ; i++){
final String videoID = videos[i];
youTubeThumbnailView = new YouTubeThumbnailView(this);
youTubeThumbnailView.setTag(videoID);
youTubeThumbnailView.initialize(API_KEY, this);
ll.addView(youTubeThumbnailView);
}
以及用于初始化
@Override
public void onInitializationSuccess(YouTubeThumbnailView thumbnailView,
YouTubeThumbnailLoader thumbnailLoader) {
youTubeThumbnailLoader = thumbnailLoader;
thumbnailLoader.setOnThumbnailLoadedListener(new ThumbnailLoadedListener());
youTubeThumbnailLoader.setVideo(thumbnailView.getTag().toString());
}
活动
public class TestActivity extends AppCompatActivity {
private static final String API_KEY = "AIzTjlIUpkoRVgchvcCzW1Aebi0PqJU_ak";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
final YouTubeThumbnailView youTubeThumbnailView = (YouTubeThumbnailView) findViewById(R.id.youtubethumb_id);
youTubeThumbnailView.setTag("UwTuL-PWxXw");
youTubeThumbnailView.initialize(API_KEY, new YouTubeThumbnailView.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubeThumbnailView youTubeThumbnailView, final YouTubeThumbnailLoader youTubeThumbnailLoader) {
youTubeThumbnailLoader.setVideo(youTubeThumbnailView.getTag().toString());
youTubeThumbnailLoader.setOnThumbnailLoadedListener(new YouTubeThumbnailLoader.OnThumbnailLoadedListener() {
@Override
public void onThumbnailLoaded(YouTubeThumbnailView youTubeThumbnailView, String s) {
youTubeThumbnailLoader.release();
}
@Override
public void onThumbnailError(YouTubeThumbnailView youTubeThumbnailView, YouTubeThumbnailLoader.ErrorReason errorReason) {
}
});
}
@Override
public void onInitializationFailure(YouTubeThumbnailView youTubeThumbnailView, YouTubeInitializationResult youTubeInitializationResult) {
}
});
}}
xml
<com.google.android.youtube.player.YouTubeThumbnailView
android:id="@+id/youtubethumb_id"
android:layout_width="match_parent"
android:layout_height="@dimen/_150sdp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />