Javascript Youtube API没有';t work--未捕获类型错误:未定义不是函数

Javascript Youtube API没有';t work--未捕获类型错误:未定义不是函数,javascript,youtube,Javascript,Youtube,这是我的代码:我试图获取youtube运行时的当前时间。然而,它总是提醒我错误消息:“UncaughtTypeError:undefined不是函数”。这使我很困惑。因为我已经定义了前面的球员。为什么不能使用该函数?除了getCurrentTime方法之外,我还测试了其他方法,比如getVolume(),它也遇到了相同的错误 有什么帮助吗?提前谢谢 <!doctype html> <html lang="en"> <head> <meta char

这是我的代码:我试图获取youtube运行时的当前时间。然而,它总是提醒我错误消息:“UncaughtTypeError:undefined不是函数”。这使我很困惑。因为我已经定义了前面的球员。为什么不能使用该函数?除了getCurrentTime方法之外,我还测试了其他方法,比如getVolume(),它也遇到了相同的错误

有什么帮助吗?提前谢谢

<!doctype html>
<html lang="en">
<head> 
  <meta charset="UTF-8"> 
  <title>THis is YOutube API testing </title>
</head>

<body>

  <div class="container">
   <iframe id="video" width="560" height="315" src="https://www.youtube.com/embed/02GcUZ6hgzo" frameborder="0" allowfullscreen></iframe>
  </div>

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

  //create the YouTube Player
  var player;

  function onYouTubeIframeAPIReady() {
    console.log("API  is Ready");
    player = new YT.Player("video", { 
    events : {
      'onReady': onPlayerReady(),
      'onStateChange': onStateChangeEvent()
      } 
    });
  }
  function onPlayerReady() {
    console.log("My plaer is onReady" );

  }
  function onStateChangeEvent(){
    setInterval(function(){
       var time = player.getCurrentTime();
      //var volume = player.getVolume();
      console.log("Get Current Time: " + time);
    },1000); 

  }
  </script>
</body>
</html>

这是YOutube API测试
//导入YouTube API脚本
var tag=document.createElement('script');
tag.src=”https://www.youtube.com/iframe_api";
var firstScriptTag=document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(标记,firstScriptTag);
//创建YouTube播放器
var播放器;
函数onyoutubeiframeapiredy(){
log(“API已准备就绪”);
player=新的YT.player(“视频”{
活动:{
“onReady”:onPlayerReady(),
“onStateChange”:onStateChangeEvent()
} 
});
}
函数onPlayerReady(){
log(“我的程序已准备就绪”);
}
函数onStateChangeEvent(){
setInterval(函数(){
var time=player.getCurrentTime();
//var volume=player.getVolume();
log(“获取当前时间:+时间”);
},1000); 
}
图片中的错误消息:
我不知道发生了什么事。当我把它放进去的时候,它似乎在工作。你能解释一下我可能遗漏了什么吗

Don't know why I have to add code if I don't see any problems with the code in question?

我刚想出来。这是因为我没有在我的URL中启用javascript

下面是关于


我刚刚进入了您创建的JSFIDLE,但我并没有看到它在控制台上显示任何内容。通常,它应该调用onready事件,对吗?听到这个消息很高兴。继续,把这个作为答案,这样其他人就会知道解决方案是什么。这对其他人来说真的很好。Youtube的新iframeAPI()文档没有明确说明要包含enablejsapi=1参数。只有不推荐的API文档才会这么说!如果您像我一样,使用新的iframeapi,并且您还手动将iframe放入html中,而不是按照他们建议的方式通过API生成iframe,那么您需要包含此参数!
Enabling the JavaScript API for a chromeless player

If your application is using a chromeless player, use the following URL to load the player in your application and enable JavaScript API handlers in the player. You can then build your own custom player controls using JavaScript:

http://www.youtube.com/apiplayer?enablejsapi=1&version=3
Enabling the JavaScript API for an embedded player

Use the following URL to load an embedded video player. In the URL, replace the string VIDEO_ID with the 11-character YouTube video ID that identifies the video that the player will show.

http://www.youtube.com/v/VIDEO_ID?version=3&enablejsapi=1