YoutubePlayer初始化失败

时间:2018-11-12 11:36:46

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

我在[how to view youtube list of a specific playlist?

处找到了此代码

我添加了YoutubeAndroidPlayerAPI.jar,但没有播放,提示“初始化YouTube播放器时发生错误” 我不知道这是怎么回事。 请帮助我

我的Java代码

public class MainActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {


public static final String API_KEY = "API KEy";
//http://youtu.be/<VIDEO_ID>
public static final String VIDEO_ID = "PlayList ID";

public void onFullscreen(boolean isFullscreen) {
    if (isFullscreen)
        playbackEventListener.onPlaying();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    /** Initializing YouTube player view **/
    YouTubePlayerView youTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtube_player);
    youTubePlayerView.initialize(API_KEY, this);


}

@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, final YouTubePlayer youTubePlayer, boolean b) {
    /** add listeners to YouTubePlayer instance **/
    youTubePlayer.setPlayerStateChangeListener(playerStateChangeListener);
    youTubePlayer.setPlaybackEventListener(playbackEventListener);
    youTubePlayer.setOnFullscreenListener(new YouTubePlayer.OnFullscreenListener() {
        @Override
        public void onFullscreen(boolean c) {
            if(c)
                youTubePlayer.play();
            else
                youTubePlayer.play();
        }
    });


    /** Start buffering **/
    if (!b) {

        youTubePlayer.loadPlaylist(VIDEO_ID);


    }
}

@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
    Toast.makeText(this, "Failured to Initialize!", Toast.LENGTH_LONG).show();
}
private YouTubePlayer.PlaybackEventListener playbackEventListener = new YouTubePlayer.PlaybackEventListener() {



    @Override
    public void onBuffering(boolean arg0) {
    }

    @Override
    public void onPaused() {
    }

    @Override
    public void onPlaying() {
    }

    @Override
    public void onSeekTo(int arg0) {
    }

    @Override
    public void onStopped() {
    }

};


private YouTubePlayer.PlayerStateChangeListener playerStateChangeListener = new YouTubePlayer.PlayerStateChangeListener() {

    @Override
    public void onAdStarted() {
    }

    @Override
    public void onError(YouTubePlayer.ErrorReason arg0) {
    }

    @Override
    public void onLoaded(String arg0) {
    }

    @Override
    public void onLoading() {
    }


    @Override
    public void onVideoEnded() {
    }

    @Override
    public void onVideoStarted() {
    }
};

}

我的XML代码

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.dfddf.ytplayerdemo.MainActivity">

<com.google.android.youtube.player.YouTubePlayerView
    android:id="@+id/youtube_player"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"/>

1 个答案:

答案 0 :(得分:1)

我尝试播放其他播放列表(更改了Playlist_ID),并且对我有用