javascript - getDuration() on youtube api is not a function? - Stack Overflow

I've been implementing the youtube API in my website, the playVideo() for example works fine, so I

I've been implementing the youtube API in my website, the playVideo() for example works fine, so I guess there is nothing wrong with the implementation.. but the getDuration() gets an error (not such function error)

this is how I try to do it :

function GetDuration() {
    var duration;
    if (ytplayer) {
        duration = ytplayer.getDuration();
    }
    $(".optionVideo").append("<p>" + duration + "</p>")
}

its error is that there is no such function. could anybody know what am I doing wrong ?

jsFiddle: /

I've been implementing the youtube API in my website, the playVideo() for example works fine, so I guess there is nothing wrong with the implementation.. but the getDuration() gets an error (not such function error)

this is how I try to do it :

function GetDuration() {
    var duration;
    if (ytplayer) {
        duration = ytplayer.getDuration();
    }
    $(".optionVideo").append("<p>" + duration + "</p>")
}

its error is that there is no such function. could anybody know what am I doing wrong ?

jsFiddle: http://jsfiddle/V36cL/6/

Share Improve this question edited Jun 25, 2012 at 17:32 gen_Eric 227k42 gold badges303 silver badges342 bronze badges asked Jun 25, 2012 at 16:56 thormayerthormayer 1,0706 gold badges29 silver badges49 bronze badges 5
  • 2 Example, jsFiddle? (Because according the Youtube API the function should exist.) – Styxxy Commented Jun 25, 2012 at 17:04
  • Without the code as Styxxy suggests, we can't know for sure. Though looking just at this code, it could be that when this function is called, ytplayer is not fully initialized or it's the wrong object. – NateDSaint Commented Jun 25, 2012 at 17:07
  • 1 @thormayer: Your jsFiddle has some errors. You forgot a ) after $(document).ready(. You forgot to include swfobject. You didn't have jQuery selected. Oh, and ytplayer is undefined. – gen_Eric Commented Jun 25, 2012 at 17:19
  • @Rocket thanks, I've update it . jsfiddle/V36cL/3 – thormayer Commented Jun 25, 2012 at 17:28
  • 1 @thormayer: Here's a better fiddle: jsfiddle/V36cL/6 :-P – gen_Eric Commented Jun 25, 2012 at 17:31
Add a ment  | 

1 Answer 1

Reset to default 4

You need to call GetDuration from your onYouTubePlayerReady, not from $(document).ready. The YouTube video may be ready after the DOM is.

Also, your onYouTubePlayerReady function needs to be global (jsFiddle wraps it in an anonymous function).

window.onYouTubePlayerReady = function(playerId) {
    window.ytplayer = document.getElementById("ytPlayer");
    GetDuration();
}

Demo: http://jsfiddle/V36cL/7/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745216647a4617055.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信