简体   繁体   English

在Safari上搜索“已结束”的中间视频的HTML5视频

[英]HTML5 video seeking to mid-video on 'ended' on Safari

I'm using a <video> for a background hero video. 我正在使用<video>作为背景英雄视频。 It starts at 0:00. 它从0:00开始。 When it reaches the end of the 0:11 clip, it pops back to :02 and repeats from there. 当它到达0:11剪辑的末尾时,它会弹回:02并从那里重复。 I've accomplished it with: 我完成了它:

document.getElementById('bgvid').addEventListener('ended',repeatVid,false);
function repeatVid(e) {
  var mediaElement = document.getElementById('bgvid');
  mediaElement.currentTime = 2;
  mediaElement.play();
}

I'm only concerned with the desktop experience, since it swaps to a fallback image on tablet/mobile. 我只关心桌面体验,因为它在平板电脑/手机上转换为后备图像。 This works with Chrome, Firefox, and even IE. 这适用于Chrome,Firefox甚至IE。 Safari, however, repeats back from 0:00 instead of :02. 然而,Safari从0:00而不是02重复。

Putting the whole function on a button onclick instead of an addeventListener works. 将整个函数放在onclick按钮而不是addeventListener Reassigning currentTime doesn't appear to be the problem. 重新分配currentTime似乎不是问题。 I've tried putting the whole thing inside video.oncanplaythrough = function () { . 我已经尝试将整个内容放在video.oncanplaythrough = function () { I've tried changing the .addEventListener('ended') to $('video').on('ended',function(){ or video.onended = function () { . 我已经尝试将.addEventListener('ended')更改$('video').on('ended',function(){video.onended = function () {

It seems like .play() always sends you back to 0:00, except I can get it to work on('click') , so there's something up. 似乎.play()总是会把你送回到0:00,除了我可以让它继续工作on('click') ,所以有一些东西。 Thanks for your help! 谢谢你的帮助!

The play() function in Safari seems to reset the currentTime to zero. Safari中的play()函数似乎将currentTime重置为零。

If you play() the video and set the currentTime directly after that, it works like expected: 如果您play()视频并在此之后直接设置currentTime,它将按预期工作:

document.getElementById('bgvid').addEventListener('ended',repeatVid,false);
function repeatVid(e) {
  var mediaElement = document.getElementById('bgvid');
  mediaElement.play();
  mediaElement.currentTime = 2;
}

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

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