3

我正在使用 YouTube Player API Reference for iframe 的简单代码

<div id="player"></div>
<script>
    //Load player api asynchronously.
    var tag = document.createElement('script');
    tag.src = "//www.youtube.com/iframe_api";
    var firstScriptTag = document.getElementsByTagName('script')[0];
    firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
    var done = false;
    var player;
    function onYouTubeIframeAPIReady() {
        player = new YT.Player('player', {
          height: '327',
          width: '530',
          videoId: '_XaWYzsY920',
          playerVars: { 'autoplay': 1,'playlist':['Hzgzim5m7oU', 'QvZIYIVNGZ0']},
          events: {
            'onReady': onPlayerReady,
            'onStateChange': onPlayerStateChange
          }
        });
    }
    function onPlayerReady(evt) {
        evt.target.playVideo();
    }
    function onPlayerStateChange(evt) {
        console.log(evt.data);
        console.log(evt.target.getVideoUrl());
    }
</script>

我想用 evt.data == 5 解决,但它从来没有被解雇过......

有办法吗?

4

2 回答 2

4

在里面onPlayerStateChange抓取视频 URL,evt.target.getVideoUrl()然后使用正则表达式提取视频 ID:

function onPlayerStateChange(evt) {
    if (evt.data == YT.PlayerState.PLAYING) {
        var url = evt.target.getVideoUrl();
        // "http://www.youtube.com/watch?v=gzDS-Kfd5XQ&feature=..."
        var match = url.match(/[?&]v=([^&]+)/);
        // ["?v=gzDS-Kfd5XQ", "gzDS-Kfd5XQ"]
        var videoId = match[1];
    }
}
于 2013-02-08T17:27:09.870 回答
0
var videoId = evt.target.getVideoData().video_id
于 2016-03-21T10:14:08.863 回答