Autoplay YouTube videos in WKWebView with iOS 11

前端 未结 5 889
小鲜肉
小鲜肉 2020-12-17 17:19

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 em

5条回答
  •  别那么骄傲
    2020-12-17 17:53

    let videoView = UIView(frame: CGRect(x: 0, y: 80, width: self.view.frame.width, height: self.view.frame.height - 80))
                self.view.addSubview(videoView)
    
    let youTubeVideoHTML: String = "  
    " let html: String = String(format: youTubeVideoHTML, videoView.frame.width, videoView.frame.height, id) let webConfiguration = WKWebViewConfiguration() if #available(iOS 9.0, *) { webConfiguration.allowsAirPlayForMediaPlayback = true webConfiguration.allowsInlineMediaPlayback = true webConfiguration.allowsPictureInPictureMediaPlayback = true webConfiguration.mediaTypesRequiringUserActionForPlayback = [] } webView = WKWebView(frame: CGRect(x: 0, y: 0, width: videoView.frame.width, height: videoView.frame.height), configuration: webConfiguration) webView.uiDelegate = self webView.scrollView.isScrollEnabled = false webView.scrollView.zoomScale = 0 webView.isMultipleTouchEnabled = false webView.scrollView.isPagingEnabled = false webView.scrollView.isMultipleTouchEnabled = false videoView.addSubview(webView) webView.loadHTMLString(html, baseURL: URL(string: "https://www.youtube.com"))

提交回复
热议问题