WKWebView视频内嵌iOS Xcode

时间:2018-06-07 20:36:36

标签: ios swift xcode wkwebview

我尝试使用WKWebView播放从youtube INLINE嵌入的视频。但是,我无法解决该问题,因为当我点击播放时,我的视频仍会自动进入全屏状态。我已经花了几个小时,但我还是找不到解决方案。我会感激你的帮助,请。这是我的代码:

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

    let videoId = "9n1e1N0Sa9k"
    if let mediaURL:URL = URL(string: "https://www.youtube.com/embed/\(videoId)?playsinline=1") {
        let request:URLRequest = URLRequest(url: mediaURL)
        myPlayer.load(request)
    }
}

1 个答案:

答案 0 :(得分:1)

这是您想要的解决方案,我以编程方式制作播放器并更改一些代码。

var myPlayer: WKWebView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let webConfiguration = WKWebViewConfiguration()
        webConfiguration.allowsInlineMediaPlayback = true
        webConfiguration.mediaTypesRequiringUserActionForPlayback = []

        myPlayer = WKWebView(frame: CGRect(x: 0, y: 0, width: 375, height: 300), configuration: webConfiguration)
        self.view.addSubview(myPlayer)

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