Android-youtube-player: Loading previous video when playing from list.

Created on 5 Jun 2019  路  7Comments  路  Source: PierfrancescoSoffritti/android-youtube-player

Using version: 8.0.1

When I am playing video from the list, first video play properly but when you select other videos it stills plays the same old playing video from start.

Log.e("Playing Video",videoId);
youTubePlayer.loadVideo(videoId,0);

I put a log just before loading video to the player and I got all the videos ids I tried to play.
After tapping on three different videos from the list I got this log for the last tap:

Playing聽Video: Ek17-Sh7jtA
Playing聽Video: rJqX-YUZddc
Playing聽Video: _cPHiwPqbqo

the first video tapped was rJqX-YUZddc
the last video tapped was Ek17-Sh7jtA

question

Most helpful comment

Thanks @Cortlandd !

All 7 comments

From what you are describing it looks like there is some bug in your code.

I am directly using this

public void playVideo(final String videoId , String song_name)
{

    tv_song_name.setText(song_name);

    playerView.initialize(new YouTubePlayerInitListener() {
        @Override
        public void onInitSuccess(@NonNull final YouTubePlayer youTubePlayer) {
            youTubePlayer.addListener(new AbstractYouTubePlayerListener() {
                @Override
                public void onReady() {
                    Log.e("Playing Video",videoId);
                    youTubePlayer.loadVideo(videoId,0);
                }
            });
        }
    }, true);
}

when tapping song on the list, the value of video id and name is passing to this function.
The first song is playing perfectly but after tapping the second time and on it is not playing properly.

Why are you initializing the player each time? You should initialize it just the first time and call just loadVideo the following times.

How can I use youTubePlayer directly from outside.

How can I use youTubePlayer directly from outside.

My code looks like the following.:

// Global
var initializedYouTubePlayer: YouTubePlayer? = null

override fun onCreate() {
    initYoutubePlayerView()
}

private fun initYoutubePlayerView() {

    playerView = findViewById(R.id.player_view)
    lifecycle.addObserver(playerView!!)

    playerView?.addYouTubePlayerListener(object: AbstractYouTubePlayerListener() {
        override fun onReady(youTubePlayer: YouTubePlayer) {
            super.onReady(youTubePlayer)
            initializedYouTubePlayer = youTubePlayer
            initializedYouTubePlayer!!.addListener(tracker)
        }
   }
}

Pass initializedYouTubePlayer anywhere to play video now. Appears you had the same issue I was having. This should solve it.

Thanks @Cortlandd !

Thanks, it helped.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hannojg picture hannojg  路  4Comments

abahl817 picture abahl817  路  4Comments

DorenBoust picture DorenBoust  路  6Comments

divyas93 picture divyas93  路  7Comments

mankidal19 picture mankidal19  路  8Comments