thormayer
thormayer

Reputation: 1070

getDuration() on youtube api is not a function?

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.net/V36cL/6/

Upvotes: 4

Views: 7301

Answers (1)

gen_Eric
gen_Eric

Reputation: 227310

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.net/V36cL/7/

Upvotes: 4

Related Questions