单击按钮即可播放不同的youtube视频。YoutubeAPI

时间:2018-11-05 16:09:19

标签: android android-studio youtube-data-api

我有两个按钮和两个播放器,在特定的按钮上单击它必须在其中一个播放器上播放视频,我是否需要在播放器上使用其他API密钥?

我正在使用的YouTube库是Android Youtube Player

这是我的Java类的代码,其中包括我尝试编写的代码

public class MainActivity extends YouTubeBaseActivity {
    private Button button;
    private Button button2;
    private YouTubePlayerView youTubePlayerView;

    private YouTubePlayer.OnInitializedListener onInitializedListener;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        youTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtube_view);
        onInitializedListener = new YouTubePlayer.OnInitializedListener() {

            @Override
            public void onInitializationSuccess(YouTubePlayer.Provider provider,
                YouTubePlayer youTubePlayer, boolean b) {
                youTubePlayer.loadVideo("YTUBE LINK");
                youTubePlayerView.initialize("API KEY", onInitializedListener);
            }

            @Override
            public void onInitializationFailure(YouTubePlayer.Provider provider,
                YouTubeInitializationResult youTubeInitializationResult) {
            }
        };

        youTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtube_view1);

        onInitializedListener = new YouTubePlayer.OnInitializedListener() {
            @Override
            public void onInitializationSuccess(YouTubePlayer.Provider provider,
                YouTubePlayer youTubePlayer, boolean b) {
                youTubePlayer.loadVideo("YTUBE LINK");
                youTubePlayerView.initialize("API KEY", onInitializedListener);
            }

            @Override
            public void onInitializationFailure(YouTubePlayer.Provider provider,
                YouTubeInitializationResult youTubeInitializationResult) {

            }
        };

        button2 = (Button) findViewById(R.id.button2);
        button2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                youTubePlayerView.initialize("API KEY", onInitializedListener);
            }
        });

        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                youTubePlayerView.initialize("API KEY", onInitializedListener);
            }
        });
    }
}

0 个答案:

没有答案