CHawk
CHawk

Reputation: 1356

Youtube API - Handling Videos that have Been Removed by Youtube

Here is the site I am working on: http://t3kno.dewpixel.net/

As you can see there is a running list of Songs (youtube videos of songs). I implemented a functionality so that once one song is done playing, the next one in the list automatically starts playing.

I am running into a problem when the video I'm trying to load is has been removed by youtube for copyright content. Currently, I'm checking the song's state by calling:

player.getPlayerState()

And waiting for the state to return 0 (ended). Once the song ends I try and load up the next song. Once that song has loaded I call:

player.playVideo()

However, if that song has been removed, I'm out of luck. I want to try and find a way to catch that event and skip to the next song. However, there is no state change when I try and play a song that has been removed. Meaning:

function onytplayerStateChange(newState) {
//do stuff
}

Is never executed. How can I detect this event and handle it correctly?

Upvotes: 4

Views: 2915

Answers (1)

Paul Graffam
Paul Graffam

Reputation: 2149

Instead of using the onStateChange try using the OnError event listener. If it returns 100 then that means the video has been removed or turned to private. Refer here for official documentation on the onError listener: https://code.google.com/apis/youtube/flash_api_reference.html#Adding_event_listener

So you would want to do something along these lines:

ytplayer.addEventListener("onError", "onPlayerError");

function onPlayerError(errorCode) {
    if(errorCode == 100)
    {
       //play next video
    }
}

You can mess around with the api and determining the best way to catch the error by going here: https://code.google.com/apis/ajax/playground/?exp=youtube#polling_the_player

Upvotes: 6

Related Questions