Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use Java to load playlist with PierfrancescoSoffritti / android-youtube-player 12.1.0? #1099

Open
pooya-mohammadhossein opened this issue Jan 20, 2024 · 8 comments
Labels

Comments

@pooya-mohammadhossein
Copy link

First of all thanks for your great works, I have managed to use [PierfrancescoSoffritti 12.1.0 to load Youtube video. I saw that you have supported Playlist Videos as well, I saw your GitHub homepage you have written the following code in Katlin for supporting Playlist , I was wondering what is the equivalent code in Java in regard of supporting playlist ? I mean what is the equivalent of following:

val iFramePlayerOptions = IFramePlayerOptions.Builder()
  .controls(1)
  .listType("playlist")
  .list(PLAYLIST_ID)
  .build()

This is the code I have used but as mentioned earlier only support Youtube Video not playlist . how should I set that the listType would be "playlist" in java , Please kindly help me to fix it to work with Playlist too. Thanks in advance.

here is the code that I need your assistant to kindly help me to fix it :


  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.youtube_player);
 playListData = Objects.requireNonNull(getIntent().getExtras())
        .getString("YoutubePlayListUri", "PlayListKey");

    youTubePlayerView = findViewById(R.id.youtubePlay);
    getLifecycle().addObserver(youTubePlayerView);
    if (playListData.startsWith("PL")) {
      youTubePlayerView.addYouTubePlayerListener(new AbstractYouTubePlayerListener() {
        @Override
        public void onReady(@NonNull YouTubePlayer youTubePlayer) {
          youTubePlayer.loadVideo(playListData, 1);
        }
      });
<com.pierfrancescosoffritti.androidyoutubeplayer.core.player.views.YouTubePlayerView
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:id="@+id/youtubePlay"
  app:enableAutomaticInitialization="true"
>

Kindest Regards,
Sincerely
Pooya

@billdizl
Copy link

IFramePlayerOptions iFramePlayerOptions = new IFramePlayerOptions.Builder().controls(1)
                .listType("playlist")
                .list(PLAYLIST_ID)
                .build();

@pooya-mohammadhossein
Copy link
Author

IFramePlayerOptions iFramePlayerOptions = new IFramePlayerOptions.Builder().controls(1)
                .listType("playlist")
                .list(PLAYLIST_ID)
                .build();

@billdzl Thank you so much for your respond. I have already Tried that but I am not sure if I miss anything I meanAlthough I am able to play Youtube Video but when I use your suggestion still I can't play Youtube Playlist. I have 2 questions from you which if you could reply I would highly appreciate it:

  1. could you please kindly clarify after your suggestion code how can I play Youtube Playlists?
  2. Should I change Anything in the Xml Layout to support Youtube Playlists?

I use following code but it can not still play Youtube PlayLists?

    youTubePlayerView.addYouTubePlayerListener(new AbstractYouTubePlayerListener() {
         @Override
         public void onReady(@NonNull YouTubePlayer youTubePlayer) {
           IFramePlayerOptions iFramePlayerOptions = new IFramePlayerOptions.Builder()
               .controls(1)
               .build();

// I am not sure how after build how can I play the Youtube Playlists (I have tried following code although I was able to run // YouTube Video but still I can't play Youtube playlist
        //   youTubePlayer.loadVideo(playListData, 1);
         }
       });

All the Best,
Kindest Regards,

@pooya-mohammadhossein
Copy link
Author

pooya-mohammadhossein commented Jan 22, 2024

Thanks again, but as you know honestly the code sample you have provided is for Kotlin , and i have written my application with Java. I have test it by the following Youtube Playlist (PL22201173A69F18EA) which is Free Youtube playlist of Stanford University. could anyone please help me with what change to the my last comment I should do for java support for the Youtube playlist? I guess I have to initialize iFramePlayerOptions but as you know unfortunately the sample code and example is written in Kotlin , not Java.
Sincerely,
Best Regards

@PierfrancescoSoffritti
Copy link
Owner

Here you go:

package com.pierfrancescosoffritti.androidyoutubeplayer.core.sampleapp.examples.playlistExample;

import android.os.Bundle;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.YouTubePlayer;
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.listeners.AbstractYouTubePlayerListener;
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.options.IFramePlayerOptions;
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.views.YouTubePlayerView;
import com.pierfrancescosoffritti.aytplayersample.R;

public class PlaylistExampleActivity extends AppCompatActivity {
    private static final String PLAYLIST_ID = "PLEpEmEcrrKJUhZkyIAgQ17Oxyd3fx_y1j";
    private YouTubePlayerView youTubePlayerView;
    private YouTubePlayer youTubePlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_playlist_example);

        youTubePlayerView = findViewById(R.id.youtube_player_view);
        IFramePlayerOptions iFramePlayerOptions = new IFramePlayerOptions.Builder()
            .controls(1)
            .listType("playlist")
            .list(PLAYLIST_ID)
            .build();

        getLifecycle().addObserver(youTubePlayerView);
        youTubePlayerView.initialize(youtubePlayerListener, true, iFramePlayerOptions);

        Button nextVideoButton = findViewById(R.id.next_video_button);
        nextVideoButton.setOnClickListener(v -> {
            if (youTubePlayer != null) {
                youTubePlayer.nextVideo();
            }
        });

        Button previousVideoButton = findViewById(R.id.previous_video_button);
        previousVideoButton.setOnClickListener(v -> {
            if (youTubePlayer != null) {
                youTubePlayer.previousVideo();
            }
        });

        Button playSecondVideoButton = findViewById(R.id.play_second_video_button);
        playSecondVideoButton.setOnClickListener(v -> {
            if (youTubePlayer != null) {
                youTubePlayer.playVideoAt(1);
            }
        });

        Button shuffleButton = findViewById(R.id.shuffle_button);
        shuffleButton.setOnClickListener(v -> {
            if (youTubePlayer != null) {
                youTubePlayer.setShuffle(true);
            }
        });

        Button loopButton = findViewById(R.id.loop_button);
        loopButton.setOnClickListener(v -> {
            if (youTubePlayer != null) {
                youTubePlayer.setLoop(true);
            }
        });
    }

    private final AbstractYouTubePlayerListener youtubePlayerListener = new AbstractYouTubePlayerListener() {
        @Override
        public void onReady(YouTubePlayer youTubePlayer) {
            PlaylistExampleActivity.this.youTubePlayer = youTubePlayer;
        }
    };
}

@pooya-mohammadhossein
Copy link
Author

Here you go:

package com.pierfrancescosoffritti.androidyoutubeplayer.core.sampleapp.examples.playlistExample;

import android.os.Bundle;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.YouTubePlayer;
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.listeners.AbstractYouTubePlayerListener;
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.options.IFramePlayerOptions;
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.views.YouTubePlayerView;
import com.pierfrancescosoffritti.aytplayersample.R;

public class PlaylistExampleActivity extends AppCompatActivity {
    private static final String PLAYLIST_ID = "PLEpEmEcrrKJUhZkyIAgQ17Oxyd3fx_y1j";
    private YouTubePlayerView youTubePlayerView;
    private YouTubePlayer youTubePlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_playlist_example);

        youTubePlayerView = findViewById(R.id.youtube_player_view);
        IFramePlayerOptions iFramePlayerOptions = new IFramePlayerOptions.Builder()
            .controls(1)
            .listType("playlist")
            .list(PLAYLIST_ID)
            .build();

        getLifecycle().addObserver(youTubePlayerView);
        youTubePlayerView.initialize(youtubePlayerListener, true, iFramePlayerOptions);

        Button nextVideoButton = findViewById(R.id.next_video_button);
        nextVideoButton.setOnClickListener(v -> {
            if (youTubePlayer != null) {
                youTubePlayer.nextVideo();
            }
        });

        Button previousVideoButton = findViewById(R.id.previous_video_button);
        previousVideoButton.setOnClickListener(v -> {
            if (youTubePlayer != null) {
                youTubePlayer.previousVideo();
            }
        });

        Button playSecondVideoButton = findViewById(R.id.play_second_video_button);
        playSecondVideoButton.setOnClickListener(v -> {
            if (youTubePlayer != null) {
                youTubePlayer.playVideoAt(1);
            }
        });

        Button shuffleButton = findViewById(R.id.shuffle_button);
        shuffleButton.setOnClickListener(v -> {
            if (youTubePlayer != null) {
                youTubePlayer.setShuffle(true);
            }
        });

        Button loopButton = findViewById(R.id.loop_button);
        loopButton.setOnClickListener(v -> {
            if (youTubePlayer != null) {
                youTubePlayer.setLoop(true);
            }
        });
    }

    private final AbstractYouTubePlayerListener youtubePlayerListener = new AbstractYouTubePlayerListener() {
        @Override
        public void onReady(YouTubePlayer youTubePlayer) {
            PlaylistExampleActivity.this.youTubePlayer = youTubePlayer;
        }
    };
}

@PierfrancescoSoffritti
Much Appreciate it. you are really and seriously great. I have searched the example folder but unfortunately Could not find the XML layout for supporting Youtube playlist. Could you please kindly provide the Xml Layout sample too? I mean regard your last respond how should I define ("R.id.next_video_button" , "R.id.previous_video_button", "R.id.next_video_button" , "R.id.play_second_video_button" , "R.id.shuffle_button" , "R.id.loop_button" , "R.id.loop_button". Thank you again. Please keep on Great Works. Sincerely,
Kindest Regards.

@billdizl
Copy link

please refer to https://github.com/PierfrancescoSoffritti/android-youtube-player/tree/master/core-sample-app ,
If you still don't know how to do it, I feel like you @pooya-mohammadhossein can give up on Android.

@PierfrancescoSoffritti ,I guess it's possible that the monkey invited it

@pooya-mohammadhossein
Copy link
Author

pooya-mohammadhossein commented Jan 23, 2024

@PierfrancescoSoffritti @billdizl
Thanks , I have managed to add Youtube playlist to my app

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants