1

我正在尝试使用适用于 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 创建多个缩略图的事实有关——但是我觉得这可能不是正确的做法。

4

1 回答 1

1

您没有为缩略图设置正确的视频。每个thumbnailView 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 中创建不同的监听器。不要在您的 thumbnailViewsthis的方法中传递,并为 clickListener 进行我向您展示的相同开关。initialze

还尝试确定您遇到的问题并仅发布相关内容。

于 2013-11-26T23:13:42.130 回答