如预期的YouTube API不玩视频 [英] YouTube API does not play videos as expected

查看:208
本文介绍了如预期的YouTube API不玩视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图建立使用Android版YouTube API一个很简单的视频播放器。到目前为止,我有一个视频(可点击并饰)及以下的两份缩略图 - 我的问题是:我需要弄清楚如何我可以(正确地)分配不同的影片每个缩略图 - 然后进行播放

I'm attempting to build a very simple video player using the Youtube API for Android. So far I have one video (which can be clicked and played) and two thumbnails below it - my problem is: I need to figure out how I can (correctly) assign different videos to each thumbnail - then play them.

我已经尝试分配缩略图所以使用:

I've attempted assign the thumbnails so using:

 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。

But for some reason I get the correct thumbnail displayed and video played for both youTubeThumbnailLoader1 and youTubeThumbnailLoader2.

有什么建议?我只是需要调整/修改低于code展现独特的视频/缩略图(而不是同一台重复多次,因为它是现在),并播放它们。

Any suggestions? I simply need to tweak/modify the code below to show unique videos/thumbnails (instead of the same one repeated multiple times as it is now) and play them.

截图

code:

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

I think this may have to do with the fact I'm using youTubeThumbnailView1 and youTubeThumbnailView2 to create multiple thumbnails - however I have a feeling this might not be the correct way of doing so.

推荐答案

您没有设置正确的视频缩略图。有每个thumbnailView clickListener里面没有区别!

You didn't set the right video for your thumbnails. There is no difference inside each thumbnailView clickListener!

尝试这样的事情在你的onCreate

try something like this in your 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();
        }
    });

}

请注意:我看到了很多乱七八糟的东西,在你的code

Note: I see a lot of messy stuff in your code.

 @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);
 }

这部分显示你所欠缺的节目基本的了解。你可以简单地这样写:

This part shows that you lack basic understanding of programming. You could simply just write:

 @Override
 public void onInitializationSuccess(YouTubeThumbnailView thumbnailView, 
   YouTubeThumbnailLoader thumbnailLoader) {

    thumbnailLoader.setOnThumbnailLoadedListener(new ThumbnailLoadedListener());
    thumbnailLoader.setVideo(VIDEO1_ID);
    thumbnailLoader.setVideo(VIDEO2_ID);

 }

这显然不可以您打算什么。我猜你有你的onCreate创建不同的听众。不通过这个您thumbnailViews的 initialze 方法内,并在同一个交换机我告诉你的clickListener。

which is clearly not what you intended. I guess you have to create different Listeners in your onCreate. Dont pass this inside the initialze method of your thumbnailViews and make the same switch I show you for the clickListener.

也尽量明确的问题,你有和后只涉及的东西。

Also Try to nail down the problem you have and post only relating stuff.

这篇关于如预期的YouTube API不玩视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆