适用于Android的Youtube API - 创建多个缩略图

时间:2013-11-20 22:44:00

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

出于某种原因,我的所有缩略图都显示相同。我不确定我做错了什么,但我相信你们其中一个人可以发现问题(这是我第一次使用Youtube API)

有什么建议吗?

P.S。

我认为这可能与我使用youTubeThumbnailView1和youTubeThumbnailView2创建多个缩略图的事实有关 - 但我有一种感觉这可能不是正确的方法。

截图:

http://i.stack.imgur.com/DEWKK.png

JAVA:

import com.google.android.youtube.player.YouTubeBaseActivity;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayerView;
import com.google.android.youtube.player.YouTubePlayer.Provider;
import com.google.android.youtube.player.YouTubeThumbnailLoader;
import com.google.android.youtube.player.YouTubeThumbnailLoader.ErrorReason;
import com.google.android.youtube.player.YouTubeThumbnailView;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ScrollView;
import android.widget.Toast;

public class Home extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener,
YouTubeThumbnailView.OnInitializedListener {

    public static final String API_KEY = "AIzaSyC0Te2pyooXzuyLaE6_SsFlITKCwjj55fI";
    public static final String VIDEO_ID = "o7VVHhK9zf0";
    public static final String VIDEO1_ID = "xVHHJqntuXI";
    public static final String VIDEO2_ID = "YWteQj_q3Ro";

    private YouTubePlayer youTubePlayer;
    private YouTubePlayerView youTubePlayerView;
    private YouTubeThumbnailView youTubeThumbnailView1;
    private YouTubeThumbnailView youTubeThumbnailView2;

    private YouTubeThumbnailLoader youTubeThumbnailLoader;
    ScrollView mainScrollView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);

        youTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtubeplayerview);
        youTubePlayerView.initialize(API_KEY, this);

        mainScrollView = (ScrollView)findViewById(R.id.groupScrollView);



        youTubeThumbnailView1 = (YouTubeThumbnailView) findViewById(R.id.youtubethumbnailview1);
        youTubeThumbnailView1.initialize(API_KEY, this);

        youTubeThumbnailView1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if (youTubePlayer != null) {
                    youTubePlayer.cueVideo(VIDEO1_ID);
                    youTubePlayer.play();
                    mainScrollView.smoothScrollTo(0, 0);
                }
            }
        });




        youTubeThumbnailView2 = (YouTubeThumbnailView) findViewById(R.id.youtubethumbnailview2);
        youTubeThumbnailView2.initialize(API_KEY, this);

        youTubeThumbnailView2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg1) {
                if (youTubePlayer != null) {
                    youTubePlayer.cueVideo(VIDEO1_ID);
                    youTubePlayer.play();
                    mainScrollView.smoothScrollTo(0, 0);
                }
            }
        });
    }

    @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) {

        youTubeThumbnailLoader = thumbnailLoader;
        thumbnailLoader
        .setOnThumbnailLoadedListener(new ThumbnailLoadedListener());

        youTubeThumbnailLoader.setVideo(VIDEO_ID);

    }

    private final class ThumbnailLoadedListener implements
    YouTubeThumbnailLoader.OnThumbnailLoadedListener {

        @Override
        public void onThumbnailError(YouTubeThumbnailView arg0, ErrorReason arg1) {

        }

        @Override
        public void onThumbnailLoaded(YouTubeThumbnailView arg0, String arg1) {

        }

    }

}

2 个答案:

答案 0 :(得分:2)

问题在于这个功能:

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

    youTubeThumbnailLoader = thumbnailLoader;
    thumbnailLoader
    .setOnThumbnailLoadedListener(new ThumbnailLoadedListener());

    //You are always loading the thumbnail of VIDEO_ID
    youTubeThumbnailLoader.setVideo(VIDEO_ID);

}

您始终加载VIDEO_ID的缩略图。您应该检查哪个视图已初始化。试试这个:

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

    youTubeThumbnailLoader = thumbnailLoader;
    thumbnailLoader
    .setOnThumbnailLoadedListener(new ThumbnailLoadedListener());

    if (thumnailView.equals(youTubeThumbnailView1)
       youTubeThumbnailLoader.setVideo(VIDEO1_ID);
    else
       youTubeThumbnailLoader.setVideo(VIDEO2_ID);

}

答案 1 :(得分:0)

您似乎正在为youTubeThumbnailView1和youTubeThumbnailView2调用youTubePlayer.cueVideo(VIDEO1_ID);