最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - player.getDuration() and player.getCurrentTime() is not function error? - Stack Overflow

programmeradmin1浏览0评论

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.

Share Improve this question edited Aug 6, 2017 at 10:10 JackHasaKeyboard 1,6951 gold badge17 silver badges30 bronze badges asked Jun 13, 2017 at 13:45 Pradeep SainiPradeep Saini 3361 gold badge3 silver badges13 bronze badges 1
  • 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
Add a ment  | 

2 Answers 2

Reset to default 3

The 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;
发布评论

评论列表(0)

  1. 暂无评论