【发布时间】:2022-01-03 15:10:08
【问题描述】:
我遇到了从嵌入式推文到 HTML 推文播放视频的问题。我正在寻找的行为很简单:有可能点击播放按钮来播放视频。但是我的应用程序可以包含来自不同资源的视频,例如Twitter、YouTube 等,我想以相同的方式播放所有视频:点击播放按钮。但是,在某些 WKWebView 配置下,twitter 视频无法播放,但其他视频可以正常播放;在另一种 WKWebView 配置下,twitter 视频可以正常播放,但同时其他视频可以自动播放,这是不正确的行为。
这是我的 WKWebView 配置和 HTML 加载的代码示例:
let configuration = WKWebViewConfiguration()
configuration.allowsInlineMediaPlayback = true
// Twitter video doesn't play, other videos can play by tap action
configuration.mediaTypesRequiringUserActionForPlayback = .all
// Twitter video doesn't play, other videos can play by tap action
// configuration.mediaTypesRequiringUserActionForPlayback = .video
// Twitter video plays correctly by tap action, but other videos can play automatically which is not correct behaviour for me
// configuration.mediaTypesRequiringUserActionForPlayback = .audio
// Twitter video plays correctly by tap action, but other videos can play automatically which is not correct behaviour for me
// configuration.mediaTypesRequiringUserActionForPlayback = []
let html = """
<html>
<body>
<blockquote class="twitter-tweet">
<p lang="en" dir="ltr">Jamie Carragher meets James Bond ????<br><br>Daniel Craig discusses his love of Liverpool, the thrill he felt meeting the players in person, his final appearance as James Bond, and whether Jurgen Klopp would make a good 007 ????⚽<a href="<some_link>">pic.twitter.com/kxYrAO3LIh</a></p>
— Sky Sports News (@SkySportsNews) <a href="https://twitter.com/SkySportsNews/status/1444597028286259203?ref_src=twsrc%5Etfw">October 3, 2021</a>
</blockquote>
<script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
</body>
</html>
"""
let webView = WKWebView(frame: .zero, configuration: configuration)
webView.loadHTMLString(html, baseURL: nil)
谢谢!
【问题讨论】: