Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

239
Visualizações
how do I loop video until user clicks. then play secondary video and revert to first video once secondary has finished, javascript, HTML

Trying to get a trailer looped until the user clicks somewhere on the screen after which the lecture video is supposed to play once and then revert to the trailer. Managed to find a similar question here on StackOverflow, unfortunately, I'm unable to get it to fully work. The trailer loops and when I click on the button the lecture video plays but does not start the trailer video and instead just loops the lecture video.

What am I doing wrong here?

<video id="mp4Source" autoplay loop controls width="100%" src="/assets/trailer.mp4" >

</video>

<button type="button" name="button" onclick="playLectureVid()"> Play Lecture Vid</button>

function playLectureVid(){
    var video =  document.getElementById("mp4Source");
    video.src = '/assets/lecture.mp4';
    video.load();
    video.play();
    video.addEventListener("ended", function(e) {
      e.currentTarget.removeEventListener(e.type, handler);
      video.src = '/assets/trailer.mp4';
      video.load();
      video.play();
    });
  }
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Just toggle between two videos.

var trailer = document.getElementById("trailer");
var lecture = document.getElementById("lecture");

function playLectureVid() {
  lecture.style.display = 'block';
  trailer.style.display = 'none';
  trailer.pause();
  lecture.play();
}

lecture.addEventListener("ended", function(e) {
  trailer.style.display = 'block';
  lecture.style.display = 'none';
  lecture.pause();
  trailer.play();
});
video {
  width: 400px;
}

#lecture {
  display: none;
}
<center>
  <button type="button" name="button" onclick="playLectureVid()"> Play Lecture Vid</button>
  <br>
  <video id="trailer" autoplay muted loop controls width="100%" src="https://jsoncompare.org/LearningContainer/SampleFiles/Video/MP4/Sample-MP4-Video-File-for-Testing.mp4"></video>
  <video id="lecture" controls width="100%" src="https://samplelib.com/lib/preview/mp4/sample-5s.mp4"></video>

</center>

about 4 years ago · Juan Pablo Isaza Relatório

0

Instead of using video.addEventListener("ended",, you can use video.onend = () => {code here}. Then, because this is called every time the video ends, insert these lines to the top of your function:

if (trailerLoaded) return
trailerLoaded = true

and add this at the top of playLectureVid:

var trailerLoaded = false

This will make it so that the video will not be reloaded every time it ends.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda