Youtube API - Handling Videos Deleted by Youtube

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

As you can see, there is a running list of songs (youtube videos of songs). I implemented the functionality, so as soon as one song is played, the next one on the list will automatically start playing.

I had a problem when the video I am trying to download was deleted by youtube for copyright content. I am currently checking the status of the song by calling:

player.getPlayerState() 

And waiting for the return of state 0 (completed). As soon as the song ends, I will try to download the next song. As soon as this song loads, I call:

 player.playVideo() 

However, if this song was deleted, I had no luck. I want to try to find a way to catch this event and move on to the next song. However, when I try to play a song that has been deleted, there is no change in state. Value:

 function onytplayerStateChange(newState) { //do stuff } 

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

+4
source share
1 answer

Instead of using onStateChange, try using an OnError event listener. If it returns 100, then the video has been deleted or closed. See the official documentation for the onError listener: https://code.google.com/apis/youtube/flash_api_reference.html#Adding_event_listener

So, you would like to do something in this direction:

 ytplayer.addEventListener("onError", "onPlayerError"); function onPlayerError(errorCode) { if(errorCode == 100) { //play next video } } 

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

+6
source

Source: https://habr.com/ru/post/1382184/


All Articles