PierfrancescoSoffritti / android-youtube-player

YouTube Player library for Android and Chromecast, stable and customizable.
https://pierfrancescosoffritti.github.io/android-youtube-player/
MIT License
3.43k stars 763 forks source link

YouTubePlayerView onReady isn't calling #1003

Open SourabhBhatt761 opened 1 year ago

SourabhBhatt761 commented 1 year ago

Hi,

Please refer to the following code, there's no change from the code mentioned in the readme.

YouTubePlayerView youTubePv = findViewById(R.id.youtube_player_fragment);
getLifecycle().addObserver(youTubePv);

Log.i(TAG,"youTube -> "+ youTubePv);

youTubePv.addYouTubePlayerListener(new AbstractYouTubePlayerListener() {
                @Override
                public void onReady(@NonNull YouTubePlayer youTubePlayer) {

                    Log.i(TAG,"onReady");
                    String videoId = videoList.get(0);
                    Log.i(TAG,"video id -> "+ videoList.get(0));
                    youTubePlayer.loadVideo(videoId,0);

                }
            });
<com.pierfrancescosoffritti.androidyoutubeplayer.core.player.views.YouTubePlayerView
        android:id="@+id/youtube_player_fragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toTopOf="@+id/bt_control"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView13"/>

onReady isn't called even though YouTubePlayerView has been initialized properly. I see the black screen with the YouTube icon and if I click on it I get the error INVALID_PARAMETER_IN_REQUEST.

Note:- with the XML if I use app:videoId="S0Q4gqBUs7c" app:autoPlay="true", the video starts playing perfectly.

Library version:- 11.1.0

Devices:- Vivo , android version 11 Oneplus 11 , android version 12

PierfrancescoSoffritti commented 1 year ago

Could it be that you're passing the wrong video id programmatically? Please have a look a the sample apps for how to use the library programmatically.

SourabhBhatt761 commented 1 year ago

No, the video id was correct as I said it was working with the xml.

I've added another lister which is

youTubePv.getYouTubePlayerWhenReady(youTubePlayer -> {
                String videoId = videoList.get(0);

                Log.i(TAG, "onReady");
                Log.i(TAG, "video id -> " + videoId);
                youTubePlayer.loadVideo(videoId, 0);

                setYouTubePlayerTracker(youTubePlayer);
            });

And now it's working fine as expected. Thanks.

PierfrancescoSoffritti commented 1 year ago

I see. Try setting enableAutomaticInitialization to false, in the xml.