admin管理员组

文章数量:1200985

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

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

Share Improve this question asked Nov 29, 2013 at 0:51 Marc SterMarc Ster 2,3166 gold badges36 silver badges64 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 20
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']);
    }
}

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

Using window.location.href...

You can paste this into the console on desktop and if you're on a youtube video it'll take the id out of the url, not elegant at all but it works for desktop

var url = window.location.href;

var isYTvid = url.indexOf("www.youtube.com/watch?v=");
if (isYTvid != -1) {
    var id = url.slice(url.indexOf("=") + 1 , url.length);
    console.log(id);
}

(Original created by Dal) - This version is edited to work with playlists

var url = window.location.href;

var isYTvid = url.indexOf("www.youtube.com/watch?v=");
if (isYTvid != -1) {
    var id = url.split("&")[0].split("=")[1]
    console.log(id);
}

本文标签: javascriptYoutube API See which video id is currently playingStack Overflow