Recent changes to youtube API for embedding multiple videos in dynamic playlist?

Viewed 795

For the last year I have been using a PHP script to build a playlist from a small collection of videos. It would build a custom playlist of the selected videos and render a set of playlist controls from the API. As of this time yesterday it was working, now it is not.

Here's the snippet that is broken:

$result = "<iframe width=\"720\" height=\"405\" src=\"https://www.youtube.com/embed/?playlist=";
$last = count($playlist) -1;
for ($k=0; $k < count($playlist); $k++){
  if ($k < $last){
    $result = $result . $playlist[$k]['videoid'] . ",";
  } else {
    $result = $result . $playlist[$k]['videoid'] . "&controls=1\" frameborder=\"0\" allowfullscreen></iframe>";
  }

I get the following error instead of an imbed with a list of playback selections:

An error occurred. Please try again later. (Playback ID: jBLBkfJOl8-X0sct)

Has something changed in the API on Aug 31, 2020? I am not finding any documentation or information that is specifically about building custom playlists in the API. I don't want to search existing playlists, I want to build a playlist based on video links that are cultivated for viewing.

I adapted my solution from this stackoverflow answer

2 Answers

I put a bounty on this question because the same issue happened to me and I was busy with other things and didn't have time to try and debug this issue on the personal project that was utilising this.

Still not entirely sure what caused this to suddenly break as I still haven't seen any notifications that youtube changed their api, but that isn't unusual when working with google product apis.

The short answer to this problem is that you now need to include an initial video id to embed before the playlist querystring.

So the embed code now needs to look like:

<iframe id="ytplayer" type="text/html" width="720" height="405"
            src="https://www.youtube.com/embed/Yc4VO8yzYcU?playlist=Y8R4Wvw7wlg,hz2HIKsQfIw,0UbCOpRye5c&playsinline=1"
            frameborder="0" allowfullscreen>

FYI: Google seems to have updated how to display a channel playlist showing whatever the most recent video is: https://support.google.com/youtube/answer/171780?hl=en

A sample embed code now needs "videoseries" included after "embed/" and before "?list":

<iframe width="560" height="315" src="https://www.youtube.com/embed/videoseries?list=PLx0sYbCqOb8TBPRdmBHs5Iftvv9TPboYG" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
Related