youtube视频结束后执行Javascript功能

时间:2012-08-28 12:59:30

标签: jquery youtube

我正在通过youtube API加载YouTube视频,因为我想在视频结束时执行一行jQuery。由于某种原因,我的代码不会执行,有人可以指出我正确的方向......

首先我嵌入了iFrame

 <iframe id="player" class="blo" width="219" height="207" src="http://www.youtube.com/embed/qNL81Khx_UE?enablejsapi" frameborder="0" allowfullscreen></iframe>

然后我创建API脚本

<script type="text/javascript">
          // 2. This code loads the IFrame Player API code asynchronously.
          var tag = document.createElement('script');
          tag.src = "http://www.youtube.com/player_api";
          var firstScriptTag = document.getElementsByTagName('script')[0];
          firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

          // 3. This function creates an <iframe> (and YouTube player)
          //    after the API code downloads.
          var player;
          function onYouTubePlayerAPIReady() {
            player = new YT.Player('player', {
              height: '390',
              width: '640',
              videoId: 'qNL81Khx_UE',
              events: {
                'onReady': onPlayerReady,
                'onStateChange': onPlayerStateChange
              }
            });
          }
          </script>

然后我做了一个功能

<script type="text/javascript">
 var myPlayerState;
  function onPlayerStateChange(event) {
    if (event.data == YT.PlayerState.PLAYING && !done) {
      alert('Something Happened');
    }
    myPlayerState = event.data;
     }

if (myPlayerState == 1){
alert('Something Happened');
}

有谁能让我知道我做错了什么?

谢谢!

0 个答案:

没有答案