Welcome to Code Forum!

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!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

Wanting to insert multiple videos in the same html player- is there a way to do this?

Hi All,

Any help on the above is very much appreciated. Any ideas?

All the best,

Eliot
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
 
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
One 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.

JS isn't my strong suit either, so I asked ai to help:

HTML:
<!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>
 
Last edited:

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom