YoutubePlayer Initialize Fail - PullRequest
       8

YoutubePlayer Initialize Fail

0 голосов
/ 12 ноября 2018

Я нашел этот код здесь [ как просмотреть список YouTube определенного списка воспроизведения?

Я добавил 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"/>

...