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/
You need to call
GetDurationfrom youronYouTubePlayerReady, not from$(document).ready. The YouTube video may be ready after the DOM is.Also, your
onYouTubePlayerReadyfunction needs to be global (jsFiddle wraps it in an anonymous function).Demo: http://jsfiddle.net/V36cL/7/