Воспроизведение видео в цикле с различным временем и функцией - PullRequest
0 голосов
/ 16 мая 2019

У меня есть 2 функции, которые воспроизводят одно и то же видео, но с разным временем. Я не могу играть, чтобы функция работала правильно.

Похоже, что функция не сбрасывает другую функцию

Я пытался изменить имена переменных, но все равно менял время щелчка.

var video = document.getElementById('videoElm');

function playShortVideo() {
  var starttime = 0; // start at 0 seconds
  var endtime = 2; // stop at 2 seconds
  video.addEventListener("timeupdate", function() {
    if (this.currentTime >= endtime) {
      this.currentTime = 0; // change time index here
    }
  }, false);

  video.load();
  video.play();
}

function playFullVideo() {
var starttime = 0; // start at 0 seconds
var endtime = 24; // stop at 2 seconds
  video.addEventListener("timeupdate", function() {
    if (this.currentTime >= endtime) {
      this.currentTime = 0; // change time index here
    }
  }, false);

  video.load();
  video.play();
}

//play short video by default
playShortVideo();


//CLICK events 
var btnshort = $('.shortvideo');
var btnfull = $('.fullvideo');
btnshort.click(function() {
  
  playShortVideo();
});

btnfull.click(function() {
  playFullVideo();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
    <video id="videoElm" autoplay muted controls loop>
    <source src="http://clips.vorwaerts-gmbh.de/VfE_html5.mp4" type="video/webm">
</video>
 
</div>

<button class="shortvideo">play 2 secs only</a><br>

<button class="fullvideo">loop full video</button>

Ответы [ 2 ]

0 голосов
/ 16 мая 2019

Это происходит потому, что вы подключаете прослушиватель события timeUpdate несколько раз.Вам нужно либо использовать один-единственный, либо удалить его перед установкой нового.

var video = document.getElementById('videoElm');
var listener;
var starttime = 0;
var endtime = 2;

function updateVideo(e) {
  if (e.target.currentTime >= endtime) {
    e.target.currentTime = 0; // change time index here
  }
}

function playShortVideo() {
  starttime = 0; // start at 0 seconds
  endtime = 2; // stop at 2 seconds
  if (!listener) {
    listener = video.addEventListener("timeupdate", updateVideo, false);
  }
  video.load();
  video.play();
}

function playFullVideo() {
  starttime = 0; // start at 0 seconds
  endtime = 24; // stop at 2 seconds
  if (!listener) {
    listener = video.addEventListener("timeupdate", updateVideo, false);
  }
  video.load();
  video.play();
}

//play short video by default
playShortVideo();


//CLICK events 
var btnshort = $('.shortvideo');
var btnfull = $('.fullvideo');
btnshort.click(function() {

  playShortVideo();
});

btnfull.click(function() {
  playFullVideo();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
  <video id="videoElm" autoplay muted controls loop>
    <source src="http://clips.vorwaerts-gmbh.de/VfE_html5.mp4" type="video/webm">
</video>

</div>

<button class="shortvideo">play 2 secs only</a><br>

<button class="fullvideo">loop full video</button>
0 голосов
/ 16 мая 2019

Это потому, что слушатель все еще там, вам нужно удалить его.

Помните, чтобы удалить его, вы не можете использовать анонимную функцию в качестве обратного вызова, поэтому я превратил ее в определенную функцию.

var video = document.getElementById('videoElm');
const playShort = function() {
  if (this.currentTime >= 2) {
    this.currentTime = 0; // change time index here
  }
};
const playFull = function() {
  if (this.currentTime >= 24) {
    this.currentTime = 0; // change time index here
  }
};

function playShortVideo() {
  video.removeEventListener("timeupdate", playFull, false)
  video.addEventListener("timeupdate", playShort, false);

  video.load();
  video.play();
}

function playFullVideo() {
  video.removeEventListener("timeupdate", playShort, false)
  video.addEventListener("timeupdate", playFull, false);
  
  video.load();
  video.play();
}

//play short video by default
playShortVideo();


//CLICK events 
var btnshort = $('.shortvideo');
var btnfull = $('.fullvideo');
btnshort.click(function() {

  playShortVideo();
});

btnfull.click(function() {
  playFullVideo();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
  <video id="videoElm" autoplay muted controls loop>
    <source src="http://clips.vorwaerts-gmbh.de/VfE_html5.mp4" type="video/webm">
</video>

</div>

<button class="shortvideo">play 2 secs only</a><br>

<button class="fullvideo">loop full video</button>

А вот подход к запуску видео через 49 секунд (60> 49 + 10)

const shortStartTime = 49;
const shortDuration = 10;

var video = document.getElementById('videoElm');
const playShort = function() {
  if (this.currentTime > (shortStartTime + shortDuration)) {
    this.currentTime = shortStartTime; // change time index here
  }
};
const playFull = function() {
  if (this.currentTime >= 24) {
    this.currentTime = 0; // change time index here
  }
};

function playShortVideo() {
  video.removeEventListener("timeupdate", playFull, false)
  video.addEventListener("timeupdate", playShort, false);

  video.load();
  video.currentTime = shortStartTime;
  video.play();
}

function playFullVideo() {
  video.removeEventListener("timeupdate", playShort, false)
  video.addEventListener("timeupdate", playFull, false);
  
  video.load();
  video.play();
}

//play short video by default
playShortVideo();


//CLICK events 
var btnshort = $('.shortvideo');
var btnfull = $('.fullvideo');
btnshort.click(function() {
  playShortVideo();
});

btnfull.click(function() {
  playFullVideo();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
  <video id="videoElm" autoplay muted controls loop>
    <source src="http://clips.vorwaerts-gmbh.de/VfE_html5.mp4" type="video/webm">
</video>

</div>

<button class="shortvideo">play 2 secs only</a><br>

<button class="fullvideo">loop full video</button>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...