N
N
Nikita Ok2017-03-29 15:41:09
YouTube
Nikita Ok, 2017-03-29 15:41:09

How to display your text if the video is not available (YouTube API)?

How to implement the output of your text instead of the iframe player, if the video in the iframe itself is not available for some reason?
The documentation has information, sort of like, that their API can work with such "features".

onError
This event is fired when an error occurs while the player is running. The API will pass the event object to the event listener function. The object's data property will contain an integer identifier for the error type. Possible values:
2 - the request contains an invalid parameter value. For example, an error occurs when you specify a video ID that is less than 11 characters long or contains invalid characters (exclamation mark, asterisk, etc.).
5 - Error playing the requested content in the HTML player or another error related to the operation of the HTML player.
100 - The requested video was not found. This error occurs if the video has been removed (for whatever reason) or marked as private.
101 - The owner of the requested video has banned its playback in built-in players.
150 is an error similar to error 101. This is a different code for error 101.

But how do you put it on your site?
Tried something like this:
<script>
  function onYouTubeIframeAPIReady() {
  player = new YT.Player('yt-frame', {
    videoId: 'M7lc1UVf-V1',
    playerVars: {
      color: 'white', 
      controls: '1', 
      iv_load_policy: '0',
      rel: '0', 
      showinfo: '0',
      start: '1',
    },
        events: {
          'onError': onPlayerError
    }
    });
}
 
function onPlayerError(event)
{
  $('#result').text('ERROR');
}
</script>
<div id="yt-frame"></div>
<h3 id="result">NOT LOADED</h3>
<script>
var tag = document.createElement('script');
 
tag.src = 'https://www.youtube.com/iframe_api';
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
</script>

But success was not crowned (

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stalker_RED, 2017-03-31
@id_15000000

Pasted your code into jsfiddle, wrote videoId: 0, pressed play - your ERROR was displayed.
Entered the videoId of an existing video, got a working player.
CHADNT?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question