使用iOS 11在WKWebView中自动播放YouTube视频 [英] Autoplay YouTube videos in WKWebView with iOS 11
问题描述
我想在我的应用中安装一个小型YouTube播放器.我发现推荐的唯一方法是将带有YouTube播放器的网页嵌入到我的应用中.因此,我使用了WKWebView并使用 autoplay 参数:https://www.youtube.com/embed/VUbsFtLkGN8?autoplay=1
.
I want to have small YouTube player in my app. The only way i found to be recommended is embeding web page with YouTube player into my app. So i used WKWebView and loaded embed YouTube player page with autoplay parameter: https://www.youtube.com/embed/VUbsFtLkGN8?autoplay=1
.
代码如下:
let webConfiguration = WKWebViewConfiguration()
let webView = WKWebView(frame: .zero, configuration: webConfiguration)
let youtubeURL = URL(string: "https://www.youtube.com/embed/VUbsFtLkGN8?autoplay=1")
webView.load(URLRequest(url: youtubeURL!))
此嵌入播放器url在桌面Safari中自动播放,但在移动Safari或WKWebView中不自动播放.我可以强迫WKWebView以某种方式自动播放视频吗?还是使用其他YouTube播放器网址?
This embed player url autoplays in desktop Safari, but does not autoplay in mobile Safari or WKWebView. Can i force WKWebView to autoplay video somehow? Or use another YouTube player url?
推荐答案
自上次回答以来,iframe API似乎已发生变化.我已经根据 iframe API参考更新了WKWebView
中加载的HTML.使用此代码,我设法在iOS11的WKWebView
中以全屏模式自动播放YouTube视频.
It seems like the iframe API has changed since the time of the previous answer. I have updated the HTML that's loaded in the WKWebView
based on the iframe API Reference. Using this code I managed to autoplay YouTube videos in fullscreen in a WKWebView
on iOS11.
class YouTubeVideoPlayerVC: UIViewController {
@IBOutlet weak var videoPlayerView: WKWebView!
var videoURL:URL! // has the form "https://www.youtube.com/embed/videoID"
var didLoadVideo = false
override func viewDidLoad() {
super.viewDidLoad()
videoPlayerView.configuration.mediaTypesRequiringUserActionForPlayback = []
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
// Size of the webView is used to size the YT player frame in the JS code
// and the size of the webView is only known in `viewDidLayoutSubviews`,
// however, this function is called again once the HTML is loaded, so need
// to store a bool indicating whether the HTML has already been loaded once
if !didLoadVideo {
videoPlayerView.loadHTMLString(embedVideoHtml, baseURL: nil)
didLoadVideo = true
}
}
var embedVideoHtml:String {
return """
<!DOCTYPE html>
<html>
<body>
<!-- 1. The <iframe> (and video player) will replace this <div> tag. -->
<div id="player"></div>
<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', {
height: '\(videoPlayerView.frame.height)',
width: '\(videoPlayerView.frame.width)',
videoId: '\(videoURL.lastPathComponent)',
events: {
'onReady': onPlayerReady
}
});
}
function onPlayerReady(event) {
event.target.playVideo();
}
</script>
</body>
</html>
"""
}
}
这篇关于使用iOS 11在WKWebView中自动播放YouTube视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!