10

I've done plenty of searching but can't find a olsution so far...

I have a Youtube playlist. I want to embed it on a website. But instead of always starting at the first video, I want it to start at either a random video, or a at least a specific video (which I can randomly select with server side code).

I have looked at the options for the iframe embed URL and can't see a parameter that allows me any control over this. Is it possible to do?

Otherwise I presume I would need to do something in JavaScript with the API. Can someone paste or point me to some example code that I could use to accomplish this as I haven't used the Youtube API before?

4 Answers 4

16

Generate a random number on your server and then use the index parameter in the url. Here's an example where the playlist will start on the 7th item by adding index=7.

<iframe width="560" height="315" src="http://www.youtube.com/embed/videoseries?list=PL9C5815B418D1508E&index=7" frameborder="0" allowfullscreen></iframe>

The other option is to use the js api, and call loadPlaylist and include the index argument. https://developers.google.com/youtube/js_api_reference#loadPlaylist

**Edit: Since Google introduced YouTube's 3.0 API, the index parameter is now zero-based. Therefore, if we wanted to start at the 7th video, we'd modify the example above using &index=6 instead of &index=7.

(Link: https://developers.google.com/youtube/iframe_api_reference#Queueing_Functions_for_Playlists)

3
  • Thanks for that, the index parameter will do the job. Although I guess I can't get a count of the number of videos (without using the API) I see that if I use an index greater than the actual number of videos it shows the first one anyway, which is fine.
    – johna
    Oct 11, 2012 at 6:31
  • sadly, the index thing is never mentioned in the API docs, even while they do a good job to keep track of changes over the years developers.google.com/youtube/player_parameters nor could i find any such feature to choose a specific video from the playlist. :( the only alternative i could find there would be using playlist parameters and manually building a comma-separated random list. in turn, that doesn't help much to my usage case. i just wanted to choose a video from the playlist, using the video id!
    – cregox
    Apr 26, 2019 at 16:37
  • 2
    index does not seem to work anymore :/
    – strix25
    Jan 18 at 17:03
2

Based on the Greg answer I make this code, and works!

`
<?php
$min=1;  $max=8;  //the number of videos on your playlist
?>
<object width="746" height="413"><param name="movie" value="http://www.youtube.com/p/CAAA1242CA3E37B4?version=3&hl=es_ES&fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param>
<?php
//in index the script randomize the numbre between $min $max
echo "
<embed src=\"http://www.youtube.com/p/CAAA1242CA3E7B4?version=3&hl=es_ES&fs=1&index=".rand($min,$max)."\" type=\"application/x-shockwave-flash\" width=\"746\" height=\"413\" allowscriptaccess=\"always\" allowfullscreen=\"true\">
</embed>
";
?>
</object>
`
1

In my context I slightly changed the coding by Mauricio:

 index=<?php print(rand(1,50)) ?>  

(I put min and max in here directly)

-1

You shoud check http://code.google.com/p/swfobject/ or http://popcornjs.org/ .

here is an example using PopCornJS: http://www.seuratt.net/rhi-popcorn/demos/videojs-playlist.html

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.