【问题标题】:How to make a Youtube video continue when page refresh页面刷新时如何使 Youtube 视频继续播放
【发布时间】:2014-04-11 23:06:51
【问题描述】:

我想知道是否可以保存当前 Youtube 视频位置,当页面刷新时,视频将从最后一个位置开始,而不是从头开始。

使用 cookie 保存最后位置或使用 GET。其实我的网 网站不会经常刷新,因为我的页面是在后台调用的 ajax并替换成一个div,但是如果用户刷新页面, 音乐将从头开始,但我宁愿让它在刷新之前继续播放。


我做了什么:

PHP 会在参数 start=

之后将 cookie 打印到 url 中
<embed src="http://www.youtube.com/v/sIR3KAfhqZQ?version=3&autoplay=1&start=<?php print($_COOKIE['YTTIME']); ?>

然后在 javascript 中我设置了一个 Timeout 来持续保存一个 integrer(seconds) cookie。

var YTT   = parseInt(getCookie("YTTIME")); // Current Video Time
var YTTL  = 802;                 // Total video length in seconds

function SaveYTTIMEtoCookie() {
    setTimeout(function(){

        if (YTT < YTVL) YTT++;   // If current time is smaller than the maximum length
        else YTT = 0;            // Or restart to 0

        setCookie("YTTIME", YTT, 1); // SetCookie is a custom function
        SaveYTTIMEtoCookie();        // Loop the SetTimeout every 1 second.
    },1000);
}

如何在JS中保存cookie:w3schools 如何在 JS 中设置超时时间:w3schools

但同时打开 2 个或更多窗口会搞砸 通过在每个页面中向 cookie 添加 +1...:/ 任何解决方案?

【问题讨论】:

  • 你可以使用动态负载

标签: javascript jquery html ajax youtube


【解决方案1】:

您可以将时间(以秒为单位)写入 URL 以在特定时间开始

http://www.youtube.com/watch?v=8SbUC-UaAxE#t=170

t 是以秒为单位的时间。

如果您使用 API,那么您可以跟踪他们在视频中的当前距离,然后始终使用该值重新加载。

但是您必须不断地在 JS 中写入 cookie 或本地存储,然后在几秒钟内重新读取该值。

【讨论】:

  • 谢谢,这完全是答案:)
猜你喜欢
  • 2013-05-23
  • 1970-01-01
  • 2013-07-27
  • 2012-11-09
  • 2021-09-18
  • 2011-01-21
  • 2016-08-17
  • 2015-02-26
  • 1970-01-01
相关资源
最近更新 更多