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

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

本文介绍了嵌入式Youtube不在UIWebView中自动播放视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在UIWebView中嵌入YouTube视频并让它自动播放。显然这不起作用。这与:

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

或者它是某种错误?

这是我的html文件:

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>


推荐答案

这不是错误,这是默认行为。 UIWebView 在没有用户干预的情况下不会自动播放任何内容,除非您设置 UIWebView mediaPlaybackRequiresUserAction 属性(默认为 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.

这篇关于嵌入式Youtube不在UIWebView中自动播放视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 14:35