嵌入式 Youtube 不会在 UIWebView 中自动播放视频

发布于 2024-11-25 06:46:19 字数 2433 浏览 0 评论 0原文

我正在尝试将 YouTube 视频嵌入 UIWebView 并使其自动播放。显然这不起作用。这是否与以下内容相关:

https://developer.apple.com/library/archive/documentation/AudioVideo/Conceptual/Using_HTML5_Audio_Video/AudioandVideoTagBasics/AudioandVideoTagBasics.html

还是某种错误?

这是我的 html 文件:

<html>
    <head>
        <script>
            document.ontouchmove=function(e){ e.preventDefault(); };

            </script>
        </head>
    <body style="margin:0px;" bgcolor="#000000">
        <!-- 1. The <div> tag will contain the <iframe> (and video player) -->
        <div id="player" style="text-align:center;"></div>

        <script>
            // 2. This code loads the IFrame Player API code asynchronously.
            var tag = document.createElement('script');
            tag.src = "http://www.youtube.com/player_api";
            var firstScriptTag = document.getElementsByTagName('script')[0];
            firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

            // 3. This function creates an <iframe> (and YouTube player)
            //    after the API code downloads.
            var player;
            function onYouTubePlayerAPIReady() {
                player = new YT.Player('player', {
                                       height: '400',
                                       width: '768',
                                       videoId: 'HYlD0KXujAk',
                                       playerVars: {'autoplay' : 1},
                                       events: {
                                       'onStateChange': onPlayerStateChange
                                       }
                                       });
            }


            // 5. The API calls this function when the player's state changes.
            //    The function indicates that when playing a video (state=1),
            //    the player should play for six seconds and then stop.
            var done = false;
            function onPlayerStateChange(event) {
            }
            function stopVideo() {
                player.stopVideo();
            }
            </script>
    </body>
</html>

I am trying to embed a youtube video in a UIWebView and have it autoplay. Apparently this does not work. Is this related to:

https://developer.apple.com/library/archive/documentation/AudioVideo/Conceptual/Using_HTML5_Audio_Video/AudioandVideoTagBasics/AudioandVideoTagBasics.html

or is it some kind of bug?

Here is my html file:

<html>
    <head>
        <script>
            document.ontouchmove=function(e){ e.preventDefault(); };

            </script>
        </head>
    <body style="margin:0px;" bgcolor="#000000">
        <!-- 1. The <div> tag will contain the <iframe> (and video player) -->
        <div id="player" style="text-align:center;"></div>

        <script>
            // 2. This code loads the IFrame Player API code asynchronously.
            var tag = document.createElement('script');
            tag.src = "http://www.youtube.com/player_api";
            var firstScriptTag = document.getElementsByTagName('script')[0];
            firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

            // 3. This function creates an <iframe> (and YouTube player)
            //    after the API code downloads.
            var player;
            function onYouTubePlayerAPIReady() {
                player = new YT.Player('player', {
                                       height: '400',
                                       width: '768',
                                       videoId: 'HYlD0KXujAk',
                                       playerVars: {'autoplay' : 1},
                                       events: {
                                       'onStateChange': onPlayerStateChange
                                       }
                                       });
            }


            // 5. The API calls this function when the player's state changes.
            //    The function indicates that when playing a video (state=1),
            //    the player should play for six seconds and then stop.
            var done = false;
            function onPlayerStateChange(event) {
            }
            function stopVideo() {
                player.stopVideo();
            }
            </script>
    </body>
</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

在你怀里撒娇 2024-12-02 06:46:20

不,这不是错误,这是默认行为。 UIWebView 在没有用户干预的情况下不会自动播放任何内容,除非您将 UIWebViewmediaPlaybackRequiresUserAction 属性设置为 NO(默认情况下是YES)。然后它就会起作用。

No this is no bug, this is default behavior. UIWebView will not play anything automatically without user intervention, except if you set UIWebView's mediaPlaybackRequiresUserAction property to NO (it's YES by default) yourself. Then it will work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文