在android中集成youtube播放列表

时间:2014-10-23 13:32:53

标签: android youtube youtube-api playlist

我想在Android中集成youtube播放列表,以便当用户打开播放列表时,它会显示该播放列表的所有视频。下面是将youtube视频集成到android的工作代码。我在loadVideo()中提供视频ID方法。但我不知道如何整合播放列表..我想知道如何继续进行?

public class YoutubeActivity2 extends YouTubeBaseActivity implements
        YouTubePlayer.OnInitializedListener {

    private YouTubePlayer YPlayer;
    private static final String YoutubeDeveloperKey = "";
    private static final int RECOVERY_DIALOG_REQUEST = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_you_tube_api);
        final ActionBar ab = getActionBar();
        ab.hide();
        YouTubePlayerView youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
        youTubeView.initialize(YoutubeDeveloperKey, this);
    }

    @Override
    public void onInitializationFailure(YouTubePlayer.Provider provider,
            YouTubeInitializationResult errorReason) {
        if (errorReason.isUserRecoverableError()) {
            errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
        } else {
            String errorMessage = String.format(
                    "There was an error initializing the YouTubePlayer",
                    errorReason.toString());
            Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == RECOVERY_DIALOG_REQUEST)
        {

            getYouTubePlayerProvider().initialize(YoutubeDeveloperKey, this);
        }
    }

    protected YouTubePlayer.Provider getYouTubePlayerProvider() {
        return (YouTubePlayerView) findViewById(R.id.youtube_view);
    }

    @Override
    public void onInitializationSuccess(Provider provider,
            YouTubePlayer player, boolean wasRestored) {
        if (!wasRestored) {
            YPlayer = player;
            YPlayer.setFullscreen(true);
            YPlayer.loadVideo("2zNSgSzhBfM");
            YPlayer.play();
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

1 个答案:

答案 0 :(得分:0)

您应该进行数据API调用以获取频道的视频,然后可以使用这些视频ID创建播放器。

以下是该电话的示例:https://github.com/youtube/yt-direct-lite-android/blob/master/app/src/main/java/com/google/ytdl/MainActivity.java#L391