如何在 WKWebView 中自动播放 youtube 视频? [英] How to autoplay youtube video in WKWebView?

查看:46
本文介绍了如何在 WKWebView 中自动播放 youtube 视频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个代码来在 WKWebView 中播放 youtube 视频.我想在加载屏幕时自动播放视频,内联视频也不应该在新屏幕中播放.下面是我的代码.

I have written a code to play youtube video in WKWebView. I want to autoplay video when a screen is loaded also the inline video should play not in the new screen. Below is my code.

 @IBOutlet weak var myPlayer: WKWebView!
 override func viewDidLoad() {
    super.viewDidLoad()

    if let videoURL:URL = URL(string: 
    "https://www.youtube.com/embed/695PN9xaEhs?playsinline=1") {
    let request:URLRequest = URLRequest(url: videoURL)
    myPlayer.load(request)
  }
 }

我已经在界面生成器中为 WKWebView 设置了配置.

I have set configuration for WKWebView in Interface builder.

任何人都可以提供在视图加载时自动播放的建议吗?

Can anyone provide a suggestion to play it automatically when the view is loaded?

推荐答案

使用 iFrame 在 WKWebview 上加载视频并编写脚本以自动播放视频.看下面的代码.

Use iFrame to load a video on WKWebview and write the script to autoplay video. see the following code.

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', {
        playerVars: { 'autoplay': 1, 'controls': 0, 'playsinline': 1 },
        height: '\(videoPlayerView.frame.height)',
        width: '\(videoPlayerView.frame.width)',
        videoId: '\(videoURL.lastPathComponent)',
        events: {
        'onReady': onPlayerReady
        }
        });
        }

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

有关详细信息,请参阅以下帖子 在 WKWebView 中自动播放 YouTube 视频iOS 11

See the following post for more info Autoplay YouTube videos in WKWebView with iOS 11

这篇关于如何在 WKWebView 中自动播放 youtube 视频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆