Created
May 12, 2021 18:37
-
-
Save epierpont/6554b1a2e7177d7aa4223144c2b7b5a8 to your computer and use it in GitHub Desktop.
JS random video src from array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<video controlslist="nodownload" autoplay muted width="100%" controls="" id="sbtvidplayer"></video> | |
<script type="text/javascript"> | |
function shuffleArray(array) { | |
var currentIndex = array.length, | |
temporaryValue, randomIndex; | |
// While there remain elements to shuffle... | |
while (0 !== currentIndex) { | |
// Pick a remaining element... | |
randomIndex = Math.floor(Math.random() * currentIndex); | |
currentIndex -= 1; | |
// And swap it with the current element. | |
temporaryValue = array[currentIndex]; | |
array[currentIndex] = array[randomIndex]; | |
array[randomIndex] = temporaryValue; | |
} | |
return array; | |
} | |
// When the document has loaded. | |
document.addEventListener("DOMContentLoaded", function() { | |
const vid = document.getElementById("sbtvidplayer"); | |
// Video src array. | |
const nextsrc = [ | |
"https://www.steamboatpilot.com/wp-content/uploads/sites/8/2021/05/stmb-vid-steamboat-horses_1.mp4", | |
"https://www.steamboatpilot.com/wp-content/uploads/sites/8/2021/05/stmb-vid-wildwestballoon.mp4", | |
"https://www.steamboatpilot.com/wp-content/uploads/sites/8/2021/03/stmb-vid-swc.mp4", | |
"https://www.steamboatpilot.com/wp-content/uploads/sites/8/2021/03/stmb-vid-celebloop.mp4", | |
"https://www.steamboatpilot.com/wp-content/uploads/sites/8/2021/03/stmb-vid-cityhowelsen.mp4", | |
"https://www.steamboatpilot.com/wp-content/uploads/sites/8/2021/03/stmb-vid-grizzle.mp4", | |
"https://www.steamboatpilot.com/wp-content/uploads/sites/8/2021/03/stmb-vid-marabou.mp4" | |
]; | |
shuffleArray(nextsrc); | |
const videoCount = nextsrc.length; | |
vid.src = nextsrc[0]; | |
var count = 0; | |
// When video has ended, start next. | |
vid.addEventListener('ended', startNextVideo, false); | |
function startNextVideo() { | |
count++; | |
// If we are end of array, reset please. | |
if (count == videoCount) { | |
count = 0; | |
} | |
vid.src = nextsrc[count]; | |
} | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment