YouTube API loadVideoById startSeconds不起作用

时间:2013-08-14 18:30:39

标签: iframe youtube youtube-api

我为我嵌入的一些YouTube视频创建了一个章节选择器。这种方法曾经有效,但最近已经停止了。我无法弄清楚发生了什么。

我正在使用他们推荐的格式,但使用loadVideoById来显示每一章

<div class="wrapper">

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

<script type="text/javascript">
  var tag = document.createElement('script');

  tag.src = "http://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', {
      width: '625',
      videoId: 'FE5jN0rqMtM',
      events: {
        'onStateChange': onPlayerStateChange    
      },
      playerVars:{
        rel: 0,
        wmode: "opaque"
      }         
    });
  }

  function onPlayerStateChange(evt) {
    if (evt.data == 0) {
          $('#video_popup').removeClass('hide_pop');
          $('#video_popup').addClass('display_pop');
    }
    else if (evt.data == -1) {
          $('#video_popup').removeClass('display_pop');
          $('#video_popup').addClass('hide_pop');
    }
    else {
          $('#video_popup').removeClass('display_pop');
          $('#video_popup').addClass('hide_pop');
    }
  }

  function chapter1() {
       player.loadVideoById({'videoId': 'FE5jN0rqMtM', 'startSeconds': 0});
  }

  function chapter2() {
       player.loadVideoById({'videoId': 'FE5jN0rqMtM', 'startSeconds': 63});
  }

  function chapter3() {
      player.loadVideoById({'videoId': 'FE5jN0rqMtM', 'startSeconds': 135});
  }

</script>

<div id="video_popup" class="hide_pop">
    <div class="video_layover">
        <div class="promo">Thank you for watching!<br /><br /></div>
        <div class="link"><a href="javascript: chapter1();">Replay Video</a></div>
    </div>
</div>  


    <div style="margin: 0 auto 20px auto; width:625px; height:98px; text-align:center;">
    <ul class="player">

            <a href="javascript: chapter1();"><li>Chapter 1</li></a>


            <a href="javascript: chapter2();"><li>Chapter 2</li></a>


            <a href="javascript: chapter3();"><li>Chapter 3</li></a>

    </ul>
    </div>

3 个答案:

答案 0 :(得分:2)

我猜这是一个错误,虽然我无法找到它记录。如果你愿意,你可以report the bug

无论如何,我认为cueVideoById是一种更好的方法,适用于所有浏览器: 示例:JS Bin

function chapter2() {      
  player.cueVideoById('FE5jN0rqMtM', 63); // BETTER WAY
  player.playVideo();
}

答案 1 :(得分:2)

如果您遇到类似&#34;类型错误的错误:ytPlayer.loadVideoById不是函数&#34;, 然后我相信你必须等待onReady事件发生。

以下是我使用的示例代码(部分):

    var ytPlayer;
    var ytPlayerIsReady = false;


    // this methods only works if defined in the global scope !! 
    window.onYouTubeIframeAPIReady = function () {
        ytPlayer = new YT.Player('ytplayer', {
            playerVars: {
                enablejsapi: 1,
                controls: 0,
                fs: 1,
                autoplay: 1,
                rel: 0,
                showinfo: 0,
                modestbranding: 1
            },
            events: {
                onReady: onReady,
                onError: onError,
                onStateChange: onStateChange
            }
        });
    };


    // youtube code for calling the iframe api
    var tag = document.createElement('script');
    tag.src = "https://www.youtube.com/iframe_api";
    var firstScriptTag = document.getElementsByTagName('script')[0];
    firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);



    function onError(event) {
        console.log("error with code" + event.data);
    }

    function onStateChange(event) {
        console.log("change state to " + event.data);
    }

    function onReady(event) {
        ytPlayerIsReady = true;
        console.log("I'm ready");
    }


    window.myVideoPlayer = {
        init: function (options) {


            // some arbitrary code...
            // the trick is to fire options.callback,
            // which contains all the logic needed



            function timeout() {
                setTimeout(function () {
                    if (false === ytPlayerIsReady) {
                        timeout();
                    }
                    else {
                        if (options.callback) {
                            options.callback();
                        }
                    }
                }, 1000);
            }
            timeout();
        }
    };


    myVideoPlayer.init({
        callback: function(){
            // now the youtube api is ready, you should be able to call
            // loadVideoById without problems (at least it worked for me)



            // ytPlayer.removeEventListener('onStateChange');
            // ytPlayer.addEventListener('onStateChange', '_myYtPlayerOnChange');
            // ytPlayer.loadVideoById({
            //        videoId: 'xxxx',
            //        startSeconds: 12
            //    });

        }
    });

答案 2 :(得分:0)

您需要在调用loadVideoById之前确保var播放器已准备就绪

If(player != null)

{ loadVideoById .. }