Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

youtube api get current video title

I have a chromeless Youtube iframe api player that plays a Youtube playlist shuffled. all works well but I would like to display the title of the currently playing video in a div underneath the player but I don't have a clue how to do this any help would be much appreciated.

I have tried other examples from stackoverflow but they were for in-line playlists and was impracticle for a youtube playlist that has 100+ videos and with my very limited script knowledge I never got them to work anyway and the google api website does not seem to cover what i'm looking for. I tried the jwplayer which gets the titles but the shuffle function is poor and plays the same videos over and over again sometimes one video multiple times. I found a script that sorts out the shuffle problem but I loose the get title function. I've decided to stick with youtube and hope that someone can help me out.

So once again I would really appreciate any help with this thanks. This is what I have

<!DOCTYPE html>
<html>
<body>

  <div id="player"></div>

    <script>

    var tag = document.createElement('script');

    tag.src = "https://www.youtube.com/iframe_api";
    var firstScriptTag = document.getElementsByTagName('script')[0];
    firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

    var player;
    function onYouTubeIframeAPIReady() {
    player = new YT.Player('player', {
    height: '390',
    width: '640',
    playerVars : { 'rel' : 0,  'showinfo' :0, 'autoplay': 1, 'controls': 0 },
    events: {
    'onReady': onPlayerReady 
    }
    });
    }

    var getRandom = function(min, max) {
    return Math.random() * (max - min) + min;
    `enter code here`};


      var playRandomTrack = function() {
      num = getRandom(0, 99);
      player.playVideoAt(num);  
      };

  function onPlayerReady(event) {
  player.loadPlaylist({'listType': 'playlist', 'list': 'PLmc_AnfxCB6t6Lwwgx3x5OxfNOWwHluU-','index': '99','startSeconds': '0','suggestedQuality': 'hd720'});
  player.setShuffle({'shufflePlaylist' : 1});
  }

</script>

</body>
</html>
like image 337
user3450928 Avatar asked Mar 29 '14 04:03

user3450928


1 Answers

For the first video add this to onPlayerReady():

var intId = setInterval( function() {
    // Check if player is initialized and the video is loaded
    if ( player ) {
        // States: 1: playing, 2: paused, 5: stopped
        if ( [ 1, 2, 5 ].indexOf( player.getPlayerState() ) >= 0 ) {
            // Set the innerText of div with id="title" to player title
            document.getElementById( "title" ).innerText = player.getVideoData().title;
            clearInterval( intId );
        }
    }
}, 100 );

and add this to playRandomTrack():

// Delay the loop because otherwise player.getPlayerState() still returns 1 because the previous video is not unloaded yet
setTimeout( function() {
    var intId = setInterval( function() {
        if ( [ 1, 2, 5 ].indexOf( player.getPlayerState() ) >= 0 ) {
            document.getElementById( "title" ).innerText = player.getVideoData().title;
            clearInterval( intId );
        }
    }, 100 );
}, 100 );

So your whole code becomes:

<!DOCTYPE html>
<html>
<body>

    <div id="player"></div>
    <div id="title"></div>
    <script>
        var tag = document.createElement( 'script' );

        tag.src = "https://www.youtube.com/iframe_api";
        var firstScriptTag = document.getElementsByTagName( 'script' )[0];
        firstScriptTag.parentNode.insertBefore( tag, firstScriptTag );

        var player;
        function onYouTubeIframeAPIReady() {
            player = new YT.Player( 'player', {
                height: '390',
                width: '640',
                playerVars: {
                    'rel': 0,
                    'showinfo': 0,
                    'autoplay': 1,
                    'controls': 0
                },
                events: {
                    'onReady': onPlayerReady
                }
            } );
        }

        var getRandom = function( min, max ) {
            return Math.random() * ( max - min ) + min;
        };

        var playRandomTrack = function() {
            num = getRandom( 0, 99 );
            player.playVideoAt( num );
            setTimeout( function() {
                var intId = setInterval( function() {
                    if ( [ 1, 2, 5 ].indexOf( player.getPlayerState() ) >= 0 ) {
                        document.getElementById( "title" ).innerText = player.getVideoData().title;
                        clearInterval( intId );
                    }
                }, 100 );
            }, 100 );
        };

        function onPlayerReady( event ) {
            player.loadPlaylist( {
                'listType': 'playlist',
                'list': 'PLmc_AnfxCB6t6Lwwgx3x5OxfNOWwHluU-',
                'index': '99',
                'startSeconds': '0',
                'suggestedQuality': 'hd720'
            } );
            player.setShuffle( {
                'shufflePlaylist': 1
            } );
            var intId = setInterval( function() {
                if ( player ) {
                    if ( [ 1, 2, 5 ].indexOf( player.getPlayerState() ) >= 0 ) {
                        document.getElementById( "title" ).innerText = player.getVideoData().title;
                        clearInterval( intId );
                    }
                }
            }, 100 );
        }
    </script>

</body>
</html>
like image 96
Gsx Avatar answered Oct 15 '22 07:10

Gsx