gpt4 book ai didi

video - WKWebView 视频不能内联播放

转载 作者:行者123 更新时间:2023-12-05 00:47:06 42 4
gpt4 key购买 nike

所以根据 Apple 的文档:https://developer.apple.com/documentation/webkit/wkwebviewconfiguration/1614793-allowsinlinemediaplayback

我应该能够使用 Swift 4 轻松内联播放视频,但无论我做什么,它总是会在 native 视频播放器中打开视频。

这是我的代码:

convenience init(style: UITableViewCell.CellStyle, reuseIdentifier: String?, url: String = "", title: String) {
self.init(style: style, reuseIdentifier: reuseIdentifier)
self.videoTitleLabel.text = title
self.urlToVideo = url
setUpUI()
setUpLayout()
webView.backgroundColor = .smalt
webView.translatesAutoresizingMaskIntoConstraints = false
webView.configuration.allowsInlineMediaPlayback = true
webView.configuration.preferences.javaScriptEnabled = true
webView.load(URLRequest(url: URL(string: self.urlToVideo + "?playsinline")! ))
}

我做错了什么?

最佳答案

通过查看人们报告的其他一些问题以及 Apple 文档中的详细信息,我认为问题在于必须在创建 WKWebView 之前设置 WKWebViewConfiguration。

来自苹果文档:

Using the WKWebViewConfiguration class, you can determine how soon a webpage is rendered, how media playback is handled, the granularity of items that the user can select, and many other options.

WKWebViewConfiguration is only used when a web view is first initialized. You cannot use this class to change the web view's configuration after it has been created.

这与苹果提供的使用 WKWebView (https://developer.apple.com/documentation/webkit/wkwebview) 的示例一致,您可以在其中看到 WKWebViewConfiguration 被传递给 WKWebView 设置。我在 playinline 中添加了作为您案例的示例。

import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate {

var webView: WKWebView!

override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webConfiguration.allowsInlineMediaPlayback = true //** Added as an example for your case
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()

let myURL = URL(string:"https://www.apple.com")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}}

如果随后无法设置该属性以避免这种混淆,这似乎更有意义,但从文档中可以看出这是它的工作原理。

关于video - WKWebView 视频不能内联播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58438820/

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com