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

问题描述 投票:1回答:1

我想在UIWebView中嵌入一个youtube视频并让它自动播放。显然这不起作用。这与以下内容有关:

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>
iphone html5 youtube-api
1个回答
-1
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.