ios - 为什么我的嵌入式 YouTube 播放器不能自动播放?

标签 ios cocoa-touch swift uiwebview youtube-api

我正在 iOS 上的 UIWebView 中尝试使用以下代码来嵌入 YouTube 视频:

<!DOCTYPE html>
<html>
    <head>
        <style type="text/css">
            body {
                margin: 0;
                padding: 0;
            }

            body, html {
                height: 100%;
                width: 100%;
            }
        </style>
    </head>
    <body>
        <iframe id="player" type="text/html" width="100%" height="100%" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1" frameborder="0"></iframe>
        <script>
            var tag = document.createElement('script');

            tag.src = "https://www.youtube.com/iframe_api";
            var firstScriptTag = document.getElementsByTagName('script')[0];
            firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

            var player;
            function onYouTubeIframeAPIReady() {
                player = new YT.Player('player', {
                    events: {
                        'onReady': onPlayerReady
                    }
                });
            }

            function onPlayerReady(event) {
                event.target.playVideo();
            }
        </script>
    </body>
</html>

我正在使用以下 Swift 代码来完成它:

let html = "<!DOCTYPE html><html><head><style type=\"text/css\"> body { margin: 0; padding: 0; } body, html { height: 100%; width: 100%; } </style> </head> <body> <iframe id=\"player\" type=\"text/html\" width=\"100%\" height=\"100%\" src=\"http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1\" frameborder=\"0\"></iframe> <script> var tag = document.createElement('script'); tag.src = \"https://www.youtube.com/iframe_api\"; var firstScriptTag = document.getElementsByTagName('script')[0]; firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', { events: { 'onReady': onPlayerReady } }); } function onPlayerReady(event) { event.target.playVideo(); } </script> </body></html>"

let width = UIScreen.mainScreen().bounds.width
let height = UIScreen.mainScreen().bounds.height
youTubeWebView = UIWebView(frame: CGRectMake(20, 120, width - 40, height-500))
youTubeWebView.mediaPlaybackRequiresUserAction = false
view.addSubview(youTubeWebView)
youTubeWebView.loadHTMLString(html, baseURL: NSBundle.mainBundle().resourceURL)

但是虽然视频加载了,但它实际上并没有像预期的那样自动播放。

最佳答案

尝试放置 ?rel=0&autoplay=1

<iframe id="player" type="text/html" width="100%" height="100%" src="http://www.youtube.com/embed/M7lc1UVf-VE?rel=0&autoplay=1" frameborder="0"></iframe>

希望能解决您的问题。

顺便说一句:不能在 IOS/Mobile 中完成

“警告:为了防止通过蜂窝网络进行未经请求的下载,费用由用户承担,嵌入式媒体无法在 iOS 上的 Safari 中自动播放——用户总是启动播放。” https://developers.google.com/youtube/iframe_api_reference#Mobile_considerations

关于ios - 为什么我的嵌入式 YouTube 播放器不能自动播放?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26985671/

相关文章:

ios - 来自 iTC 的无效 bundle 错误,无法提交应用程序 - 因为 4 个 swift dylibs 无法嵌入

ios - 如何修复错误 : init(boundsSize:requestHandler:)’ is only available on iOS 10. 0

ios - 火力地堡错误 : "cannot assign type User? to type User?"

ios - 无法删除 Apple WWDRC 证书

ios - 每次旋转组件时,UIPickerView 似乎都在泄漏

ios - UIDatePicker的默认日期不是今天的默认日期

objective-c - TTPickerTextField 示例

ios - 在 MKMapView 之上使用 OpenGL?

ios - 如何让按钮从下午 3 点到晚上 9 点打开?

ios - 设置 UIImage 动画的正确/干净的方法?