【问题标题】:Refreshing page with background video带有背景视频的刷新页面
【发布时间】:2019-02-24 01:44:42
【问题描述】:

在我的网站上,在后台播放视频。当我要转到另一个页面时,如何在同一点制作恢复视频?使用 ajax 刷新页面。 我试图通过主页上的小脚本解决它:

<script>
var vid = document.getElementById("video");
vid.addEventListener("canplay", function(e) {
  var currTime = this.currentTime;
  this.play();
 }, false
);
</script>

另一个脚本,在另一个 html 页面上,我想继续我的视频:

<script>
var vid = document.getElementById("myVideo");
    vid.addEventListener("canplay", function(e) {
    this.currentTime = currTime;
    this.play();
 }, false
 );
</script>

我收到下一个错误:

Uncaught ReferenceError: currTime is not defined
at HTMLVideoElement.<anonymous>

我对这个解决方案是否正确?如果我能修复这个错误,它会起作用吗?如果回答:是的,我怎样才能全球化这个“currTime”? 谢谢。

更新: 视频的 HTML 代码:

<video loop muted autoplay poster="../static/images/wallpaper.png" class="fullscreen-bg__video" id="video"> <source src="../static/videos/wallpaper.mp4" type="video/mp4"> <source src="../static/videos/wallpaper.webm" type="video/webm"> </video>

.fullscreen-bg__video {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    margin:0;
}

在页面上,我正在暂停视频相同的代码,但具有另一个 id。

【问题讨论】:

  • 您可能希望连续读取视频上的当前时间戳并将其保存到 localStorage。然后,当页面重新加载时,检查 localStorage 中的时间戳并将视频加载到那个时间。
  • @daddygames 是的,听起来,正是我需要的。我怎么能这样做?通过创建 js 文件并读取时间戳并将其写入这些文件?

标签: javascript ajax html html5-video video-player


【解决方案1】:
<script>
var vid = document.getElementById("video");
vid.addEventListener("canplay", function(e) {

localStorage.setItem("videoTime", this.currentTime);
  this.play();
 }, false
);
</script> 

注意:localStorage.setItem("videoTime", this.currentTime) 只执行 ONCE。您可以将setInterval() 每秒的时间设置为一个新值。

setInterval(() => {localStorage.setItem("videoTime", this.currentTime);},1000);

重新加载后获取项目:

        <script>
    var vid = document.getElementById("myVideo");
        vid.addEventListener("canplay", function(e) {
        if(localStorage.getItem("videoTime") !== null) {
           this.currentTime = localStorage.getItem("videoTime");
        }

        this.play();
     }, false
     );
    </script>

更新:

在我的机器上测试过。适用于任何 .html 文件。只需将其粘贴为脚本:

<script>
        window.onload = () => {
            var vid = document.getElementById("video");
            if(localStorage.getItem("videoTime") !== null && localStorage.getItem("videoTime") !== undefined) {
                vid.currentTime = localStorage.getItem("videoTime");   }                  

setInterval(() => {localStorage.setItem("videoTime", vid.currentTime);},1000); }

        </script> 
  1. 在窗口加载后执行脚本。
  2. 获取 vid 作为 HTMLElement
  3. 检查是否存在带有密钥vidTime 的localStorage 条目。
  4. 如果是,用vid.currentTime = localStorage.getItem("videoTime");设置视频时间
  5. 每秒更新一次新视频时间:setInterval(() =&gt; {localStorage.setItem("videoTime", vid.currentTime);},1000);

【讨论】:

  • 效果很好,但刷新视频暂停后,我尝试了this.playing();,但它仍然暂停。
  • $('myVideo').trigger('play'); 没有帮助
  • $('myVideo').get(0).play(); 也无济于事
  • if(this.pause()){ this.play(); }没有帮助
  • @AlexanderVasilchuk 添加了更新。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-01
  • 1970-01-01
  • 2014-04-07
  • 1970-01-01
  • 1970-01-01
  • 2013-05-31
  • 1970-01-01
相关资源
最近更新 更多