全屏播放Youtube视频,然后重定向

时间:2014-06-19 19:36:08

标签: youtube-api

在使用Youtube api完成播放后,我无法重定向youtube视频。我有使用样式的CSS代码,但在这种情况下这并不重要。任何肝脏将不胜感激。编辑:我希望这是响应。这是我的代码:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">
<HTML>
  <HEAD>
    <LINK href="style.css" rel="stylesheet" type="text/css">
  </HEAD>
  <BODY>
   <div class="video-container">
         <iframe src="http://www.youtube.com/embed/sOS9aOIXPEk?autoplay=1" frameborder="0" width="560" height="315"></iframe>
</div>
</HTML>

    <div id="player"></div>
    <script>
    var tag = document.createElement('script');
    tag.src = "http://www.youtube.com/iframe_api";
    var firstScriptTag = document.getElementsByTagName('script')[0];
    firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

        // create youtube player
        var player;
        function onYouTubePlayerAPIReady() {
            player = new YT.Player('player', {
             height=''
             width=''
              videoId: '0Bmhjf0rKe8',
              playerVars:{'rel':0,'showinfo':1,'autoplay':1}
              events: {

                'onStateChange': onPlayerStateChange
              }
            });
        }



        // when video ends
        function onPlayerStateChange(event) {        
            if(!event.data)          
                GoHome();
        }
        function GoHome(){
            window.location = 'http://www.google.com';
        }


    </script>

1 个答案:

答案 0 :(得分:1)

当视频结束event.data == 2.你也错过了一些逗号作为对象条目的分隔符。 这是一个工作示例(注意:第一个iframe被注释掉了):

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">
<HTML>
  <HEAD>
    <LINK href="style.css" rel="stylesheet" type="text/css">
  </HEAD>
  <BODY>
<!--
   <div class="video-container">
    <iframe src="http://www.youtube.com/embed/sOS9aOIXPEk?autoplay=1" frameborder="0" width="560" height="315"></iframe>
</div>
-->
    <div id="player"></div>

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

        // create youtube player
        var player;
        function onYouTubePlayerAPIReady() {
            player = new YT.Player('player', {
               height: '768',
               width: '1024',
               videoId: '0Bmhjf0rKe8',
               playerVars:{'rel':0,'showinfo':1,'autoplay':1},
               events: {
                'onStateChange': onPlayerStateChange
              }
            });
        }



        // when video ends
        function onPlayerStateChange(event) {
          //alert (event.data);
          if (event.data == 0)
                  GoHome();
        }
        function GoHome(){
            window.location = 'http://www.google.com';
        }

    </script>
</BODY>
</HTML>