简体   繁体   English

两个视频之间的平滑过渡

[英]Smooth transition between two videos

Supposed to happen应该发生

I have two video elements.我有两个视频元素。 The first one should run for 6 seconds and then fadeOut and start the second video that will also run from 0 to 6 seconds.第一个应该运行 6 秒,然后淡出并启动第二个视频,该视频也将运行 0 到 6 秒。 After the 6 seconds have passed, it should fadeOut and the first one should fadeIn and run again. 6 秒过后,它应该淡出,第一个应该淡入并再次运行。 The idea is that I want that transisition to be sort of a neat fade instead of an abrupt jump.这个想法是我希望这种过渡是一种巧妙的淡入淡出而不是突然的跳跃。 This loop needs to go on until the user stop it.这个循环需要一直持续到用户停止它。 For now, my only concern is to reproduce the loop and the fade.现在,我唯一关心的是重现循环和淡入淡出。

I have我有

A piece of HTML and JavaScript.一段 HTML 和 JavaScript。 My HTML mark up simply contains two video elements within a container:我的 HTML 标记只包含一个容器中的两个视频元素:

HTML: HTML:

<div id="container" style="height: 230px; position: relative;">
   <video id="video1" style="position: absolute; top: 0; left: 0;display: none; width: 450px; height: 450px;">
      <source src="mysource.mp4" type="video/mp4">
   </video>
   <video id="video2" style="position: absolute; top: 0; left: 0; display: none; width: 450px; height: 450px;">
      <source src="mysource.mp4" type="video/mp4">
   </video>
<div id="container" style="height: 230px; position: relative;">

JavaScript: JavaScript:

var toggleTo = 2;
function startVideo(vid){
  var startTime = 0;
  var endTime = 8;
  var video = $('#video'+ vid).get(0);
  var video2 = $('#video'+ toggleTo).get(0);

  video.addEventListener('timeupdate', function(){
      console.log('vid1: '+ parseInt(this.currentTime,10));
      if(parseInt(this.currentTime,10) >= parseInt(endTime - 2)){
          video2.play();
          $('#video'+ toggleTo).fadeIn(1000);
          $('#video'+ vid).fadeOut(2000);
          }

      if(this.currentTime >= endTime){
          video.pause();
          }
      },false);

  video2.addEventListener('timeupdate', function(){
      console.log('vid2: '+ parseInt(this.currentTime,10));
      if(parseInt(this.currentTime,10) >= parseInt(endTime - 2)){
          $('#video'+ vid).fadeIn(1000);
          $('#video'+ toggleTo).fadeOut(2000);
          video.currentTime = 0;
          video.play();
          }

      if(this.currentTime >= endTime){
          video2.pause();
          }
      },false);

  video.currentTime = 0;
  video.play(); 
  }

This happens有时候是这样的

Video is starting fine and goes on until 6 seconds, where the second one is going to start.视频开始正​​常并持续到 6 秒,第二个将开始。 However, instead of pausing the first one, both videos run at the same time.但是,不是暂停第一个,而是两个视频同时运行。

What am I doing wrong?我究竟做错了什么? How could I make this work?我怎么能让这个工作? Any Ideas?有任何想法吗?

Thanks in advance!提前致谢!

Edit编辑

Came up with this.. did the trick.想出这个..成功了。 Not sure, if it's the best way though.不确定,如果这是最好的方法。

var hasEntered = false;
var hasEntered2 = false;
function startVideo(vid){
  var startTime = 0;
  var endTime = 13;

  video.addEventListener('timeupdate', function(){
     if(parseInt(this.currentTime,10) >= parseInt(endTime - 2)){
          if(hasEntered === false){
              hasEntered2 = false;
              video2.currentTime = 0;
              video2.play();
              $('#video2').fadeIn(2000);
              $('#video1').fadeOut(3000);
              hasEntered = true;
              }
          }

      if(this.currentTime >= endTime){
          video.pause();
          }
      },false);


  video2.addEventListener('timeupdate', function(){
      if(parseInt(this.currentTime,10) >= parseInt(endTime - 2)){
          if(hasEntered2 === false){
              hasEntered = false;
              $('#video1').fadeIn(2000);
              $('#video2').fadeOut(3000);
              video.currentTime = 0;
              video.play(); // and work!!
              hasEntered2 = true;
              }
          }

      if(this.currentTime >= endTime){
          video2.pause();
          }
      },false);


  video.currentTime = 0;
  video.play();
  }

You could try changing the class of the video container to another class and have a css animation linked to that class.您可以尝试将视频容器的类更改为另一个类,并将 css 动画链接到该类。 Let me know if you like this idea and I will post code.如果您喜欢这个想法,请告诉我,我会发布代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM