Javascript 如何恢复嵌入式YouTube视频?

Javascript 如何恢复嵌入式YouTube视频?,javascript,php,video,youtube,youtube-iframe-api,Javascript,Php,Video,Youtube,Youtube Iframe Api,我使用//www.youtube.com/embed/JDcaMVwCr3c?wmode=opaque&showinfo=0&autoplay=1&controls=0&modestbranding=1&vq=&rel=0url在我的网站中嵌入视频 现在,如果用户访问该站点,视频将被播放,但是如果用户没有观看完整的视频并重新加载页面,或者没有离开该站点并在一段时间后再次返回,那么视频将从一开始播放 我想从用户离开的位置恢复视频。就像YouTube网站一样 这是YouTube提供的API吗?请参考

我使用
//www.youtube.com/embed/JDcaMVwCr3c?wmode=opaque&showinfo=0&autoplay=1&controls=0&modestbranding=1&vq=&rel=0
url在我的网站中嵌入视频

现在,如果用户访问该站点,视频将被播放,但是如果用户没有观看完整的视频并重新加载页面,或者没有离开该站点并在一段时间后再次返回,那么视频将从一开始播放

我想从用户离开的位置恢复视频。就像YouTube网站一样


这是YouTube提供的API吗?

请参考以下示例并给出注释

<html>
<head>
</head>
<body>
    <div id="ytplayer"></div>
    <script>
        // Load the IFrame Player API code asynchronously.
        var tag = document.createElement('script');
        tag.src = "https://www.youtube.com/player_api";
        var firstScriptTag = document.getElementsByTagName('script')[0];
        firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

        // Replace the 'ytplayer' element with an <iframe> and
        // YouTube player after the API code downloads.
        var player;
        function onYouTubePlayerAPIReady() {
            player = new YT.Player('ytplayer', {
                height: '390',
                width: '640',
                videoId: 'M7lc1UVf-VE',  // Youtube video ID
                events: {
                    'onReady': onPlayerReady,
                    'onStateChange': onPlayerStateChange,
                }

            });

        }

        function onPlayerStateChange() {
            createCookie('ply_time', player.getCurrentTime(), 1);  // Stats like buffer, Pause and play store time in Cookes 

        }

        function onPlayerReady() {
            player.seekTo(readCookie('ply_time'));  // On ready get ccokies  and start vide from that.
        }

        document.unload = function() {                              // On docucment unload set cookie
            createCookie('ply_time', player.getCurrentTime(), 1);
        }

        window.onbeforeunload = function() {              // On Window unload set cookie
            createCookie('ply_time', player.getCurrentTime(), 1);
        }


        /* 
         * Start:-  function to create , read and erase Cookie 
         */

        function createCookie(name, value, days) {
            if (days) {
                var date = new Date();
                date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
                var expires = "; expires=" + date.toGMTString();
            }
            else
                var expires = "";
            document.cookie = name + "=" + value + expires + "; path=/";
        }

        function readCookie(name) {
            var nameEQ = name + "=";
            var ca = document.cookie.split(';');
            for (var i = 0; i < ca.length; i++) {
                var c = ca[i];
                while (c.charAt(0) == ' ')
                    c = c.substring(1, c.length);
                if (c.indexOf(nameEQ) == 0)
                    return c.substring(nameEQ.length, c.length);
            }
            return null;
        }

        function eraseCookie(name) {
            createCookie(name, "", -1);
        }

        /* 
         * End:-  function to create , read and erase Cookie 
         */

    </script>    
</body>

//异步加载IFrame播放器API代码。
var tag=document.createElement('script');
tag.src=”https://www.youtube.com/player_api";
var firstScriptTag=document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(标记,firstScriptTag);
//用and替换“ytplayer”元素
//YouTube播放器的API代码下载后。
var播放器;
函数onYouTubePlayerAPIReady(){
player=新的YT.player('ytplayer'{
高度:“390”,
宽度:“640”,
videoId:'M7lc1UVf VE',//Youtube视频ID
活动:{
“onReady”:onPlayerReady,
“onStateChange”:onPlayerStateChange,
}
});
}
函数onPlayerStateChange(){
createCookie('ply_time',player.getCurrentTime(),1);//在Cookes中存储缓冲、暂停和播放时间等属性
}
函数onPlayerReady(){
player.seekTo(readCookie('ply_time'));//在准备好的ccokies上,从那个开始视频。
}
document.unload=函数(){//On docucument unload set cookie
createCookie('ply_time',player.getCurrentTime(),1);
}
window.onbeforeunload=函数(){//On window unload set cookie
createCookie('ply_time',player.getCurrentTime(),1);
}
/* 
*开始:-创建、读取和擦除Cookie的函数
*/
函数createCookie(名称、值、天数){
如果(天){
变量日期=新日期();
date.setTime(date.getTime()+(天*24*60*60*1000));
var expires=“;expires=“+date.togmString();
}
其他的
var=”;
document.cookie=name+“=”+value+expires+“path=/”;
}
函数readCookie(名称){
变量nameEQ=name+“=”;
var ca=document.cookie.split(“;”);
对于(变量i=0;i