在Android应用中使用YouTube片段

时间:2017-02-21 11:10:11

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

我一直收到以下错误 - “网络出现了问题[400]”。

请参阅以下代码。

 private void initializeYoutubeFragment() {
        // in original code I am using my key here for sake of key safety I have replaced it
        String apiKey = "MY_KEY";
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        YouTubePlayerSupportFragment youtubeFragment = YouTubePlayerSupportFragment.newInstance();
        transaction.add(R.id.fl_video, youtubeFragment);
        transaction.commitNow();
        manager.executePendingTransactions();
        youtubeFragment.initialize(apiKey, new YouTubePlayer.OnInitializedListener() {
            @Override
            public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
                youTubePlayer.cuePlaylist("5xVh-7ywKpE");
                youTubePlayer.setPlayerStateChangeListener(callback);
            }

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

                // no callback here
                Log.e("TAG", "Fail");

            }
        });

    }

请参阅随附的屏幕截图 enter image description here

更新1 我还在onInitializationSuccess方法中将YouTubePlayer.PlayerStateChangeListener添加到youtubeplayer实例。它调用onLoading方法然后继续调用onError(YouTubePlayer.ErrorReason)。 Heer ErrorReason.INTERNAL_ERROR即使在docs

中也没有任何解释

回调代码

private YouTubePlayer.PlayerStateChangeListener callback = new YouTubePlayer.PlayerStateChangeListener() {
    @Override
    public void onLoading() {
        Log.e("Tag", "loading");
    }

    @Override
    public void onLoaded(String s) {
        Log.e("Tag", "loaded");
    }

    @Override
    public void onAdStarted() {
        Log.e("Tag", "ad started");
    }

    @Override
    public void onVideoStarted() {
        Log.e("Tag", "Video Started");
    }

    @Override
    public void onVideoEnded() {
        Log.e("Tag", "video ended");
    }

    @Override
    public void onError(YouTubePlayer.ErrorReason errorReason) {
        Log.e("Tag", "Error");
    }
};

0 个答案:

没有答案