Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you make sure that Vimeo video exist?

So, I'm trying to show a Vimeo video only if it exist. I'm using new JavaScript API.

As per their documentation, error event should get triggered when a video face errors while loading. I believe, adding a wrong Vimeo video URL should also trigger the error event.

This is what I did to get the error event in action:

<iframe id="vimeo-player1" src="https://player.vimeo.com/video/13333693532?autoplay=0&amp;background=1" frameborder="0" webkitallowfullscreen="" mozallowfullscreen="" allowfullscreen=""></iframe>

Approach 1

player = new Vimeo.Player($('#vimeo-player1'));

player.on('error', function() {
    console.log('Error in loading video');
});

Approach 2

player = new Vimeo.Player($('#vimeo-player1'));

player.loadVideo().then(function(id) {
    console.log('loaded');
}).catch(function(error) {
    console.error(error);
});

None of them working. It never executed the error block.

Some Additional Information (To help you to win Bounty):

  • Client side solution is required (I don't have access to the server side of the portal)
  • Videos are hosted by third party users
like image 407
Sanjay Joshi Avatar asked May 18 '17 11:05

Sanjay Joshi


Video Answer


1 Answers

The easiest way that I found is to call the Vimeo API using the oEmbed open standard:

function checkIfVideoExists(url, callback){
    $.ajax({
        type:'GET',
        url: 'https://vimeo.com/api/oembed.json?url=' + encodeURIComponent(url),
        dataType: 'json',
        complete: function(xhr) {
            callback(xhr.status);
        } 
    });
}

checkIfVideoExists("https://vimeo.com/217775903", function(status){
    console.log(status); // 200 - OK
});

checkIfVideoExists("https://vimeo.com/234242343", function(status){
    console.log(status); // 404 - Not found
});

Live example on jsFiddle.

like image 198
Etienne Martin Avatar answered Sep 17 '22 12:09

Etienne Martin