如何恢复嵌入的 YouTube 视频? [英] How to resume embedded YouTube video?

查看:43
本文介绍了如何恢复嵌入的 YouTube 视频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I used //www.youtube.com/embed/JDcaMVwCr3c?wmode=opaque&showinfo=0&autoplay=1&controls=0&modestbranding=1&vq=&rel=0 url to embed a video in my site.

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

Now, if a user visits the site, the video will be played, but if the user doesn't watch the full video and reload page or leave the site and come back again after some time then the video is now playing from the beginning.

我想从用户离开的地方继续播放视频.与 YouTube 网站的方式相同.

I would like to resume the video from the point which the user has left it. In the same way as the YouTube site does.

这是 YouTube 提供的 API 吗?

Is this something that YouTube provides with an API?

推荐答案

请参考下面的例子进行评论.

Please refer to the following example with comment.

<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>

这篇关于如何恢复嵌入的 YouTube 视频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆