Youtube API doesn't work--Uncaught TypeError: undefined is not a function
Youtube API doesn't work--Uncaught TypeError: undefined is not a function
这是我的代码:我正在尝试获取 youtube 运行时的当前时间。但是,它总是提醒我错误信息:"Uncaught TypeError: undefined is not a function"。这让我很困惑。因为我前面已经定义了player。为什么无法使用该功能?除了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>
图中错误信息:
我不确定发生了什么。当我把它放在 JSFiddle 中时,它似乎起作用了。你能解释一下我可能遗漏的更多信息吗:
Don't know why I have to add code if I don't see any problems with the code in question?
我刚刚弄明白了。这是因为我没有在 URL 中启用 javascript。
上的描述
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
这是我的代码:我正在尝试获取 youtube 运行时的当前时间。但是,它总是提醒我错误信息:"Uncaught TypeError: undefined is not a function"。这让我很困惑。因为我前面已经定义了player。为什么无法使用该功能?除了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>
图中错误信息:
我不确定发生了什么。当我把它放在 JSFiddle 中时,它似乎起作用了。你能解释一下我可能遗漏的更多信息吗:
Don't know why I have to add code if I don't see any problems with the code in question?
我刚刚弄明白了。这是因为我没有在 URL 中启用 javascript。
上的描述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