Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.
SignUp Now!
Sounds like you could dynamically read from a file where the urls will be contained, read them into an array in Javascript, iterate through the collection, create a new video element and put the urls into the video source attributeHi All,
Any help on the above is very much appreciated. Any ideas?
All the best,
Eliot
One at a time, I assume. Track when a video has ended and swap out the source for the next video.Sounds like you could dynamically read from a file where the urls will be contained, read them into an array in Javascript, iterate through the collection, create a new video element and put the urls into the video source attribute
The most performance optimized way is to read them all at once, and then you control which video is nextOne at a time, I assume. Track when a video has ended and swap out the source for the next video.
So how would I do this? I'm not that hot on JS.The most performance optimized way is to read them all at once, and then you control which video is next
So how would I do this? I'm not that hot on JS.
<!DOCTYPE html>
<body>
<video id="player" width="640" height="360" controls></video>
<script>
// URL list
const playlist = [
"video1.mp4",
"video2.mp4",
"video3.mp4"
];
const delayBetweenVideos = 2000; // delay between videos. Can be 0.
const loopPlaylist = true; // true = loop back to start, false = stop
const video = document.getElementById("player");
let currentIndex = 0;
function playVideo(index) {
if (playlist.length === 0) return; // nothing to play
currentIndex = index % playlist.length; // wrap around
video.src = playlist[currentIndex];
video.play().catch(err => {
console.warn("Autoplay prevented:", err);
});
}
video.addEventListener("ended", () => {
currentIndex++;
if (currentIndex < playlist.length) {
setTimeout(() => playVideo(currentIndex), delayBetweenVideos);
} else if (loopPlaylist) {
setTimeout(() => playVideo(0), delayBetweenVideos);
} else {
console.log("Playlist finished.");
}
});
// Start the first video
playVideo(currentIndex);
</script>
</body>
Code Forum is a community platform where coding enthusiasts can connect with other developers, engage in discussions, ask for help, and share their knowledge with a supportive community. It's a perfect place to improve your coding skills and to find a community of like-minded individuals who share your passion for coding.
We use essential cookies to make this site work, and optional cookies to enhance your experience.