YouTube JS API: Detect “embedding has been disabled”


Using the youtube javascript api (, I am trying to allow a user to embed a video into some content he creates in my app. I have gotten everything working, except for being able to detect and handle the case that embedding is not allowed for the video.

Currently, the player loads and shows a thumbnail of the disallowed video, and it only gives an error once the user tries to play it. This is bad because the user may not play the video before saving / sending his content. I would like to preemptively detect that the video is not allowed to be embedded, and display a helpful message to the user.

The only solution I can see is to actually play it (programmatically) and handle the error that is raised at that point.

Existing workaround:

  1. Embed player (embedSWF)
  2. onYouTubePlayerReady(): add onError onStateChange event listeners.
  3. onStateChange(newState): when the video finishes loading, try to play it.
    e.g. if (newState == 5 /* CUED */) { player.mute(); player.playVideo(); player.stopVideo(); player.unMute(); }
  4. onError(error): if the video failed to play in onStateChange, will receive error here.

Is there a better way?


Best Solution

You can grab the JSON feed for that video entry before you embed it and see if "yt$format":5 exists, which is the embed SWF. It won't be there if embedding is disabled.