0

I've been searching for a way to make my embedded playlist start with a random video.

This is what I tried:

<iframe src="https://www.youtube.com/embed/videoseries?list=PLPmj00V6sF0s0k3Homcg1jkP0mLjddPgJ&index=<?php print(rand(1,11)) ?>?rel=0&autoplay=1&showinfo=0&controls=0&authide=0&iv_load_policy=3&?modestbranding=1" frameborder="0"  allowfullscreen></iframe>

Sadly, this does not work for some reason. Neither did it with echo. Other solutions (as well few on stackoverflow https://shrty.top/j) did not work either. Any ideas?

6
  • is it a troll or why do i get the Troll Vid when i try ur Code?
    – KikiTheOne
    Oct 12, 2016 at 12:29
  • Possible duplicate of Embedding Youtube playlist but starting from random video
    – KikiTheOne
    Oct 12, 2016 at 12:31
  • It is a playlist with funny videos. This is no troll. Try it with a different playlist, it won't work either. With this code it shows the same video all the time (Rickroll)
    – Najm
    Oct 12, 2016 at 13:12
  • Nice timing, also encountering this today. Using the modern iFrame API (the only non-deprecated one) I'm not seeing a way to do this :(
    – Pixeltramp
    Oct 12, 2016 at 22:23
  • Vincent P's answer here gets you 90% of the way, EXCEPT it starts on the first video and shuffles afterwards. Might be a good starting point? Let me know if you figure out how to shuffle on initial load! stackoverflow.com/questions/36035124/…
    – Pixeltramp
    Oct 13, 2016 at 0:00

1 Answer 1

4

Got it. This works:

<html>

<head>
    <script>
        var videos = ["https://www.youtube.com/embed/9bZkp7q19f0", "https://www.youtube.com/embed/dQw4w9WgXcQ"];
        window.onload = function () {
            var playerDiv = document.getElementById("random_player");
            var player = document.createElement("IFRAME");
            var randomVideoUrl = videos[Math.floor(Math.random() * videos.length)];
            player.setAttribute('width', '640');
            player.setAttribute('height', '390');
            player.setAttribute('src', randomVideoUrl);
            playerDiv.appendChild(player);
        };
    </script>
</head>

<body>
    <div id="random_player" />
</body>

</html>

1
  • 2
    While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. Jan 29, 2017 at 18:06

Your Answer

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.