I have read similar questions posted but none did help my case.
Here is my YouTube iframe
:
<iframe id="myframe" style="border: solid 4px #37474F; ">
</iframe>
I added the src
attribute dynamically.
Here is my player:
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('myframe', {
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
I am trying to do player.getCurrentTime();
and player.getDuration();
but both keep giving the "function not defined" error.
I have read similar questions posted but none did help my case.
Here is my YouTube iframe
:
<iframe id="myframe" style="border: solid 4px #37474F; ">
</iframe>
I added the src
attribute dynamically.
Here is my player:
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('myframe', {
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
I am trying to do player.getCurrentTime();
and player.getDuration();
but both keep giving the "function not defined" error.
- Maybe these links help you YouTube Player API: getDuration(), getCurrentTime(), getVideoData() not working // How to get duration of a loaded/cued video without playing it? – Mathiasfc Commented Jun 13, 2017 at 13:55
2 Answers
Reset to default 3The YouTube player API sets player.getCurrentTime
not in the constructor but later (possibly when the video is loaded). Likely, this is a bug.
To see that this is true add console.log(player.getCurrentTime)
before each call. You will see that this is undefined
initially and later changes to a function.
Work around this by testing whether player.getCurrentTime
exists. If not, assume a return value of 0.0
. Example:
var videoPos = !player.getCurrentTime ? 0.0 : player.getCurrentTime();
The same problem seems to happen with getDuration
.
As usr said, it's likely a bug. And here's another solution:
var yourVariable = player.getCurrentTime() || 0;
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745224120a4617381.html
评论列表(0条)