Safari 11/YouTube API错误.快速播放/暂停和自动播放失败

Mat*_*ick 12 html javascript youtube safari

这刚开始被用户报告给我.我花了很多时间来探索我自己的代码以查找错误,但它似乎与Safari 11 (最新版本)有关.

使用YouTube IFrame嵌入API的简单示例时,Safari会在播放和暂停状态之间快速切换,直到暂停结束.

这不是示例的最优化版本,因为这里有一些探索可以使它工作的内容.我想跳过并自动播放,但它不会像预期的那样工作.我试着用startplayVideo被记录的YT API的例子.

我最近才证实这是一个错误,它解释了为什么示例中有一些冗长的参数.

笔记:

  • 有时视频会根据您刷新的次数而播放,但这种情况很少见.
  • 自动播放标志通常会失败.
  • start在此示例中使用标记向前跳过,因为startSeconds无法正常工作.
  • 代码示例工程在其他浏览器:Chrome,Opera,Firefox

这是你在Safari的控制台中看到的图像,它显示玩家状态的恐慌,最终登陆2(暂停).在控制台恐慌

这是一个复制/粘贴代码示例,将复制该错误.将其放在任何HTML文件中,您应该会在Safari 11中看到它失败.

<style>
    body, html, iframe {
        position: absolute; left: 0; right: 0; top: 0; bottom: 0; width: 100%; height: 100%;
        margin: 0;
        padding: 0;
        pointer-events: none;
    }
</style>


<script>
    var videoId = "9nwQ1F7oX-8";

    var playerVars = {
        autohide: 1,
        autopause: 0,
        autoplay: 1,
        cc_load_policy: "0",
        disablekb: 1,
        enablejsapi: 1,
        iv_load_policy: 1,
        modestbranding: 1,
        origin: "*",
        rel: 0,
        showinfo: 0,
        start: 122,
        version: 3
    };
</script>


<iframe id="ytplayer"
    frameborder="0"
    allowfullscreen="1"
    title="YouTube video player"
    width="100%"
    height="100%"
    x-src="https://www.youtube.com/embed/9nwQ1F7oX-8?enablejsapi=1&amp;origin=*&amp;rel=0&amp;version=3&amp;iv_load_policy=3&amp;modestbranding=1&amp;showinfo=0&amp;autohide=1&amp;disablekb=1&amp;autoplay=1&amp;autopause=0&amp;cc_load_policy=0&amp;startSeconds=30&amp;widgetid=1"
    src="https://www.youtube.com/embed/9nwQ1F7oX-8?enablejsapi=1&amp;origin=*&amp;start=122">
</iframe>


<script>
window.onYouTubeIframeAPIReady = function() {
    console.log("YouTube is ready!", videoId, playerVars);

    var api = new YT.Player("ytplayer", {
        width: "100%",
        height: "100%",
        videoId: videoId,
        playerVars: playerVars,
        events: {

            onError: function(e) {
                // 100 – The video requested was not found. This error occurs when a video has been removed (for any reason) or has been marked as private.
                // 101 – The owner of the requested video does not allow it to be played in embedded players.
                // 150 – This error is the same as 101. It"s just a 101 error in disguise!

                console.warn("An error has occurred", arguments);
            },

            onReady: function() {
                // log
                console.log("YouTube player is ready to use");

                //
                api.playVideo();
            },

            onStateChange: function(e) {
                // log
                console.log("YouTube state change ", e);

                // Finished
                if (e.data == 0) {
                    console.log("Finished");
                }

                // Playing
                else if (e.data === YT.PlayerState.PLAYING) {
                    console.log("Playing");
                }

                // Pausing
                else if (e.data === 2) {
                    console.log("Pausing");
                }

                // Buffering
                else if (e.data === 3) {
                    console.log("Buffering");
                }
            }
        }
    });

}
</script>

<script src="https://www.youtube.com/iframe_api"></script>
Run Code Online (Sandbox Code Playgroud)

小智 1

我在视频播放器方面遇到了许多问题,特别是在不同的浏览器和不同的设备中自动播放。

\n\n

似乎自动播放功能和 API 播放/暂停互相干扰,导致玩家状态恐慌。

\n\n

在我的案例中效果最好的最终解决方案:

\n\n

使用playerVars中的'autoplay: 0'将自动播放设置为off。\n'api.playVideo();' 您在“onReady: function()”中使用的应该从那里获取它并将播放器带入 \xc2\xb4play\xc2\xb4 状态。

\n