vimeo API - Store value in variable

382 views Asked by At

I'm struggling with Vimeo's Froogaloop API.

I'm working on a web-based game and I need that, before a user enters the next level, he watches an entire video (not longer than 3 minutes).

I thought I could use a while loop where as long as the current time's value is less than the video's duration, it keeps getting the current time to evaluate. When the values match, a button that links to the next level is unlocked. To do so, I have to store the getCurrentTime and getDuration values inside javascript variables.

So far it seems easy but apparently Froogaloop needs callback function with get methods and I can't manage to save them properly into variables.

function getDuration(playerID,value) {
            Froogaloop(playerID).api('getDuration',function(dur) {
                    value(dur);
                });    
            }

            var durata = getDuration(playerID,function(value){
                          var length = value;
                          return(length);
                         });
}

But this way the "durata" variable returns undefined. Am I missing anything?

Thank you

1

There are 1 answers

0
Brad Dougherty On

Because the API is asynchronous, you cannot return the value synchronously. If you're playing the video until the end, I'd recommend using the finish event instead of testing the current time and duration.

That would look something like this:

$f(playerID).addEvent('finish', function() {
    // video has finished playing here
    goToNextLevel();
});