Rob
Rob

Reputation: 1365

YouTube JS API: Detect "embedding has been disabled"

Using the youtube javascript api (http://code.google.com/apis/youtube/js_api_reference.html), 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?

Thanks!
-Rob

Upvotes: 4

Views: 4252

Answers (3)

chitzui
chitzui

Reputation: 4098

I think that you might be looking for the videoSyndicated or the videoEmbeddable parameter. The API documentation says:

videoEmbeddable

The videoEmbeddable parameter lets you to restrict a search to only videos that can be embedded into a webpage. If you specify a value for this parameter, you must also set the type parameter's value to video.

Reference: https://developers.google.com/youtube/v3/docs/search/list#videoEmbeddable

videoSyndicated

The videoSyndicated parameter lets you to restrict a search to only videos that can be played outside youtube.com. If you specify a value for this parameter, you must also set the type parameter's value to video.

Reference: https://developers.google.com/youtube/v3/docs/search/list#videoSyndicated

Example Call

With both:

GET https://www.googleapis.com/youtube/v3/search?&part=snippet,statistics&videoSyndicated=true&videoEmbeddable=true&key=${yourKey}

Upvotes: 1

Martti Laine
Martti Laine

Reputation: 12985

I know this isn't directly to the question, but in case someone using PHP stumbles upon this problem, there's a method getNoEmbed() in a Zend_Gdata_YouTube_VideoEntry.

Taken from the docs:

If the return value is an instance of Zend_Gdata_YouTube_Extension_NoEmbed, this video cannot be embedded.

Upvotes: 0

Steph Liu
Steph Liu

Reputation: 161

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.

http://gdata.youtube.com/feeds/api/videos/video_id?v=2&alt=json-in-script

Upvotes: 6

Related Questions