我有一些代码可以将网络视频加载到 SwiftUI 视图中并允许其播放。目前,视频在加载时会自动播放,我想停止这种行为。此外,该视频只能以全屏模式观看,我不知道如何制作它,以便它可以在不全屏模式下播放。
我也收到警告:
“获取断言时出错:
我尝试过这条线
webView.configuration.allowsInlineMediaPlayback = true
允许视频在非全尺寸的情况下播放,但它不起作用。如果有人知道如何使这两项功能正常工作,我将不胜感激。
import SwiftUI
import WebKit
struct YouTubeView: UIViewRepresentable {
let videoId: String
func makeUIView(context: Context) -> WKWebView {
let webView = WKWebView()
webView.configuration.allowsInlineMediaPlayback = true
webView.configuration.mediaTypesRequiringUserActionForPlayback = []
return webView
}
func updateUIView(_ uiView: WKWebView, context: Context) {
guard let demoURL = URL(string: "https://s1.fwmrm.net/m/1/169843/20/89977492/AIDP7266000H_ENT_MEZZ_HULU_8166176_578.mp4") else { return }
uiView.scrollView.isScrollEnabled = false
uiView.load(URLRequest(url: demoURL))
}
}
struct VideoPlayerView: View {
var ids = "hzls6ZUHCYM"
var body: some View {
ZStack {
ScrollView(showsIndicators: false) {
VStack {
YouTubeView(videoId: ids)
.frame(width: 300, height: 175)
}
}
}
}
}
//modifier correct YouTube struct
struct YouTubeView: UIViewRepresentable {
var videoID: String
func makeUIView(context: Context) -> WKWebView {
let webConfiguration = WKWebViewConfiguration()
webConfiguration.allowsInlineMediaPlayback = true
return WKWebView(frame: .zero, configuration: webConfiguration)
}
func updateUIView(_ uiView: WKWebView, context: Context) {
let embedHTML = """
<!DOCTYPE html>
<html>
<body>
<iframe width="800" height="550" src="https://www.youtube.com/embed/\(videoID)" frameborder="0" allowfullscreen></iframe>
</body>
</html>
"""
uiView.loadHTMLString(embedHTML, baseURL: nil)
}
}
尝试加载嵌入 html 中的视频,而不是直接加载到 mp4 url。为
playsinline
标签设置 video
参数。还要在实例化 allowsInlineMediaPlayback
对象之前设置 WKWebView
。
至于权利错误,其他答案似乎表明这可以简单地忽略。
let html = """
<video
src="https://s1.fwmrm.net/m/1/169843/20/89977492/AIDP7266000H_ENT_MEZZ_HULU_8166176_578.mp4"
width="640" height="480"
controls
playsinline="true">
"""
struct YouTubeView: UIViewRepresentable {
func makeUIView(context: Context) -> WKWebView {
let webConfiguration = WKWebViewConfiguration()
webConfiguration.allowsInlineMediaPlayback = true //set up config first
return WKWebView(frame: .zero, configuration: webConfiguration)
}
func updateUIView(_ uiView: WKWebView, context: Context) {
uiView.loadHTMLString(html, baseURL: nil) //load video embedded inside html
}
}
有时您需要创建一个解决方法
这是 ChatGPT 是如何修复它的
let webConfiguration = WKWebViewConfiguration()
// Default settings
webConfiguration.allowsInlineMediaPlayback = true
if #available(iOS 10.0, *) {
webConfiguration.mediaTypesRequiringUserActionForPlayback = .all
}
// Check if the URL contains ".mp4"
if url.lowercased().contains(".mp4") {
// Prevent fullscreen for video elements
webConfiguration.preferences.isElementFullscreenEnabled = false
// Inject JavaScript to prevent autoplay
let scriptSource = """
var videos = document.querySelectorAll('video');
videos.forEach(video => {
video.autoplay = false;
video.controls = true;
});
"""
let script = WKUserScript(source: scriptSource, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
let contentController = WKUserContentController()
contentController.addUserScript(script)
webConfiguration.userContentController = contentController
}
// Initialize the webView with the configuration
let customFrame = CGRect(origin: .zero, size: CGSize(width: 0.0, height: self.wv_Container.frame.size.height))
self.webView = WKWebView(frame: customFrame, configuration: webConfiguration)
webView.translatesAutoresizingMaskIntoConstraints = false
webView.allowsLinkPreview = true