Search code examples
androidandroid-youtube-api

Android Youtube SDK - Know when first frame is loaded


I want to show a loading screen while initializing YouTube player, but hide this screen only when the first frame of the video is loaded and shown to the user.

This is because if I remove the screen onInializationSuccess there are other few seconds in wich the player remains black. After that the player loads the first frame or video image poster, and shows the play button. I want to detect this scenario if possible. Thanks.


Solution

  • There is a YouTubePlayer.PlayerStateChangeListener interface that you can listen for this purpose.

    player.setPlayerStateChangeListener(new YouTubePlayer.PlayerStateChangeListener() {
    
            @Override
            public void onVideoStarted() {  }
    
            @Override
            public void onVideoEnded() { }
    
            @Override
            public void onLoading() {   }
    
            @Override
            public void onLoaded(String arg0) { }
    
            @Override
            public void onError(ErrorReason arg0) { }
    
            @Override
            public void onAdStarted() { }
        });
    

    onLoaded tells when the video has finally been loaded, so this is the moment where the play button and the video poster appears on screen