Search code examples
androidandroid-youtube-api

how to check if user watched whole video using YouTube api


Is there any way to find out if the user watched the whole video or not..I want to update my database if the user watches the whole video. Please help with this. PS: I don't actually care if the user skipped to the end or watched the whole thing.


Solution

  • I did find a way to do that using the following listener, the method i was looking for was onVideoEnded().

    private PlayerStateChangeListener playerStateChangeListener = new PlayerStateChangeListener() {
    
            @Override
            public void onAdStarted() {
            }
    
            @Override
            public void onError(ErrorReason arg0) {
            }
    
            @Override
            public void onLoaded(String arg0) {
            }
    
            @Override
            public void onLoading() {
            }
    
            @Override
            public void onVideoEnded() {
            }
    
            @Override
            public void onVideoStarted() {
            }
        };
    

    and then in method onInitializationSuccess(Provider provider, YouTubePlayer player, boolean wasRestored) just add the above listener to player:

    player.setPlayerStateChangeListener(playerStateChangeListener);
    

    I hope this helps :)