从自动播放Swift UI停止视频

hjzp0vay  于 2023-05-27  发布在  Swift
关注(0)|答案(1)|浏览(125)

你好,我有一些代码,可以加载一个网络视频到一个swift的用户界面视图,并允许它播放。目前,视频自动播放时,它加载,我想停止这种行为。此外,视频只能在全屏模式下观看,我不知道如何使它可以发挥,而不是在全屏。我还收到警告“获取Assert时出错:<Error Domain=RBSServiceErrorDomain Code=1“(originator doesn't have entitlement com.apple.runningboard.assertions.webkit AND originator doesn't have entitlement”I tried the line“webView.configuration.allowsInlineMediaPlayback = true”to allow the video to play while not being full sized and it doesn't work.如果有人知道如何让这2件功能的工作,我会很感激,谢谢。

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)
                }
            }
        }
    }
}
qvtsj1bj

qvtsj1bj1#

尝试加载你的视频嵌入在html而不是直接到一个mp4网址.设置video标记的playsinline参数。在示例化WKWebView对象之前也设置allowsInlineMediaPlayback
至于授权错误,other SO answers似乎表明这可以简单地忽略。

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
    }
}

相关问题