YouTube API无法按预期播放视频

时间:2013-11-20 18:07:17

标签: java android youtube youtube-api android-youtube-api

我正在尝试使用适用于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播放了视频。

有什么建议吗?我只需要调整/修改下面的代码,以显示独特的视频/缩略图(而不是像现在这样重复多次)并播放它们。

Screenshot

代码:

    @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创建多个缩略图的事实有关 - 但我有一种感觉这可能不是正确的方法。

1 个答案:

答案 0 :(得分: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中创建不同的听众。不要在缩略图视图的this方法中传递initialze,并为clickListener制作相同的开关。

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