Youtube API: See which video id is currently playing

10,354

Solution 1

player.getVideoData()['video_id']

OR

var video_data = player.getVideoData()
video_data['video_id']

Solution:

function onPlayerStateChange(event) {
    if(event.data === 0) {    
        player.stopVideo();
        player.loadVideoById(player.getVideoData()['video_id']);
    }
}

Solution 2

You can use getVideoUrl to get the url and take the id part out of this url.

Share:
10,354
Marc Ster
Author by

Marc Ster

Updated on June 26, 2022

Comments

  • Marc Ster
    Marc Ster almost 2 years

    Is there a way to display/see/return which video (so, which videoID) is playing at the moment?

    This is the basic code of the Iframe API:

    var player;
      function onYouTubeIframeAPIReady() {
        player = new YT.Player('player', {
          height: '390',
          width: '640',
          videoId: 'pJ18QeQy0e8',
          events: {
            'onReady': onPlayerReady,
            'onStateChange': onPlayerStateChange,
            'onError': onError,
          },
            playerVars: {
    
                        'controls': 0,
                        'showinfo': 0,
                        'iv_load_policy': 3,
                        'wmode': "opaque",
                    },
        });
      }
    

    after the first video has finished. This event will be called:

      function onPlayerStateChange(event) {
    if(event.data === 0) {    
            player.stopVideo();
    player.loadVideoById(getId());          
    
            }
      }
    

    The getId() function will get another videoId to play..

    I want to display in a DIV, which videoId is currently playing