我正在尝试使用Youtube API为Android构建一个非常简单的视频播放器。到目前为止,我有一个视频(可以点击播放)和下面的两个缩略图——我的问题是:我需要弄清楚如何(正确地)为每个缩略图分配不同的视频——然后播放它们。
我已经尝试分配缩略图,所以使用:
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和YouTubethumbnailLoader 2显示了正确的缩略图并播放了视频。
有什么建议吗?我只需要调整/修改下面的代码,就可以显示独特的视频/缩略图(而不是像现在这样重复多次)并播放它们。
屏幕截图
代码:
@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) {
}
}
}
p.S.
我认为这可能与我使用youTubeThumbnailView1和youTubeThumbnailView2创建多个缩略图有关,但我觉得这可能不是正确的方法。
您没有为缩略图设置正确的视频。每个拇指钉内部没有区别查看clickListener!
在onCreate 中尝试这样的操作
public void onCreate(Bundle icicle){
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(){
if(youTubePlayer != null){
youTubePlayer.loadVideo(VIDEO1_ID)
youTubePlayer.play();
}
});
youTubeThumbnailView2.setOnClickListener(new OnClickListener(){
if(youTubePlayer != null){
youTubePlayer.loadVideo(VIDEO2_ID)
youTubePlayer.play();
}
});
}
注意:我在你的代码中看到了很多乱七八糟的东西。
@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);
}
这部分表明您对编程缺乏基本的理解。你可以简单地写:
@Override
public void onInitializationSuccess(YouTubeThumbnailView thumbnailView,
YouTubeThumbnailLoader thumbnailLoader) {
thumbnailLoader.setOnThumbnailLoadedListener(new ThumbnailLoadedListener());
thumbnailLoader.setVideo(VIDEO1_ID);
thumbnailLoader.setVideo(VIDEO2_ID);
}
这显然不是您想要的。我想你必须在onCreate中创建不同的监听器。不要在thumbnailViews的initialze
方法中传递this
,并对clickListener进行与我显示的相同的切换。
也试着确定你的问题,只发布相关的东西。