我正在尝试使用适用于 Android 的 Youtube API 构建一个非常简单的视频播放器。到目前为止,我有一个视频(可以单击和播放)和它下面的两个缩略图 - 我的问题是:我需要弄清楚如何(正确地)为每个缩略图分配不同的视频 - 然后播放它们。
我尝试使用以下方法分配缩略图:
public static final String VIDEO1_ID = "HNtBphqE_LA";
public static final String VIDEO2_ID = "YWteQj_q3Ro";
public static final String VIDEO3_ID = "Ak--L4bYly0";
但由于某种原因,我为 youTubeThumbnailLoader1 和 youTubeThumbnailLoader2 显示了正确的缩略图并播放了视频。
有什么建议么?我只需要调整/修改下面的代码以显示独特的视频/缩略图(而不是像现在一样重复多次)并播放它们。
代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
youTubePlayerView = (YouTubePlayerView)findViewById(R.id.youtubeplayerview);
youTubePlayerView.initialize(API_KEY, this);
youTubeThumbnailView1 = (YouTubeThumbnailView)findViewById(R.id.youtubethumbnailview1);
youTubeThumbnailView1.initialize(API_KEY, this);
youTubeThumbnailView2 = (YouTubeThumbnailView)findViewById(R.id.youtubethumbnailview2);
youTubeThumbnailView2.initialize(API_KEY, this);
youTubeThumbnailView1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
if(youTubePlayer != null){
youTubePlayer.play();
}
}});
youTubeThumbnailView2.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
if(youTubePlayer != null){
youTubePlayer.play();
}
}});
}
@Override
public void onInitializationFailure(Provider provider,
YouTubeInitializationResult result) {
}
@Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player,
boolean wasRestored) {
youTubePlayer = player;
if (!wasRestored) {
player.cueVideo(VIDEO_ID);
}
}
@Override
public void onInitializationFailure(YouTubeThumbnailView thumbnailView,
YouTubeInitializationResult error) {
}
@Override
public void onInitializationSuccess(YouTubeThumbnailView thumbnailView,
YouTubeThumbnailLoader thumbnailLoader) {
youTubeThumbnailLoader1 = thumbnailLoader;
thumbnailLoader.setOnThumbnailLoadedListener(new ThumbnailLoadedListener());
youTubeThumbnailLoader1.setVideo(VIDEO1_ID);
youTubeThumbnailLoader2 = thumbnailLoader;
thumbnailLoader.setOnThumbnailLoadedListener(new ThumbnailLoadedListener());
youTubeThumbnailLoader2.setVideo(VIDEO2_ID);
}
private final class ThumbnailLoadedListener implements
YouTubeThumbnailLoader.OnThumbnailLoadedListener {
@Override
public void onThumbnailError(YouTubeThumbnailView arg0, ErrorReason arg1) {
}
@Override
public void onThumbnailLoaded(YouTubeThumbnailView arg0, String arg1) {
}
}
}
附言
我认为这可能与我使用 youTubeThumbnailView1 和 youTubeThumbnailView2 创建多个缩略图的事实有关——但是我觉得这可能不是正确的做法。