Android-youtube-player: Change videoID in the same YouTubePlayerView

Created on 10 Mar 2020  路  6Comments  路  Source: PierfrancescoSoffritti/android-youtube-player

Hello,

I made have fitness application.
In the app have Activity of exercises.

When it init first he show the first exercise.
When i click on "next button" all the exercise change (image, title, youtube video).

I have addYouTubePlayerListener(new AbstractYouTubePlayerListener) and the method "onReady" there the videoID change to the correct video of the exercise.

When the Activity just start and he take the first exercise onReady change to the correct video.

I put on the "next button" also addYouTubePlayerListener(new AbstractYouTubePlayerListener) and the method "onReady" and then i put the new videoID but its still show me the first video.

What i miss here?

thanks

Most helpful comment

You need to reuse the same instance of YouTubePlayer.

All 6 comments

I am also facing same problem

send your code

You need to reuse the same instance of YouTubePlayer.

Hey thanks for reply using same instance I resolved my problem !!.

I don't understand how to do. Can u explain the way you did ? @PierfrancescoSoffritti

@batoan2898 I am giving my piece of code might be it will help you have to use single youtube player instance and use cue method:-

  class VideoViewHolder extends RecyclerView.ViewHolder {

    private YouTubePlayerView youTubePlayerView;

    VideoViewHolder(@NonNull View itemView) {
      super(itemView);
      youTubePlayerView = itemView.findViewById(R.id.youtube_player_view_precautions);
      yourFragment.getLifecycle().addObserver(youTubePlayerView);
    }

    private void setDetails(Precautions precautions) {
      youTubePlayerView.addYouTubePlayerListener(new AbstractYouTubePlayerListener() {
        @Override
        public void onReady(@NonNull YouTubePlayer youTubePlayer) {
          String videoId = videoId;
          youTubePlayer.cueVideo(videoId, 0f);
        }
      });
    }
  }

If any concern feel free to ask:)

Was this page helpful?
0 / 5 - 0 ratings