Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"This video is unavailable." in Youtube IFrame API - Why, and how to fix?

I have a Youtube IFrame embed that I'm loading with the IFrame API via JS. When I try playing certain videos (it seems to usually be ones containing copyrighted music, although this isn't always the case), it simply displays "This video is unavailable." with no further message. It isn't a region restriction, as when I try to load those, that specific error is shown. These are videos that play just fine on the normal Youtube page so I have no idea what's causing this error. Is there a way to get these videos to play in the IFrame embed, or at all? I'm not entirely committed to the IFrame API method, getting these videos to play is my #1 priority.

EDIT: I was able to solve this by specifying a videoId in my YT.Player constructor. I'm seeing consistent behavior that the videos will be unavailable when a videoId is not specified on initialization but they will play if one is specified.

like image 481
scatter Avatar asked Nov 18 '22 07:11

scatter


1 Answers

Seems like the ID should only be alphanumeric. I had a ? in my src (see code below) and that stopped it from loading correctly.
Otherwise yeah like OP said it's probably missing the ID.

const videos = {}; // object containing all relevant videos on page.

const src = 'xyz'; // embed short ID
const uid = 'abc'; // unique ID that I can reference later.
const width = 560;
const height = 315;

// https://developers.google.com/youtube/player_parameters.html?playerVersion=HTML5
const options = {
    fs: 0,
    controls: 0,
    disablekb: 1,
    autoplay: 1,
    loop: 1,
    mute: 1,
    playlist: src,
    modestbranding: 1,
    playsinline: 1,
    showinfo: 0,
    autohide: 0,
    rel: 0,
    cc_load_policy: 0,
    iv_load_policy: 3,
    enablejsapi: 1,
    origin: window.location.origin,
};

videos[`${src}-${uid}`].ref = new window.YT.Player(`iframe-${src}-${uid}`, {
    videoId: src,
    playerVars: options,
    width,
    height,
});
like image 156
Richard Herries Avatar answered Nov 24 '22 00:11

Richard Herries