在通过 WKWebView 加载的视频上为 SwiftUI 启用内联媒体播放

Tho*_*Kim 4 xcode swiftui

下面是我目前拥有的代码。如何为 swiftUI 启用内联媒体播放?我希望视频在出现网络视图时自动播放。下面是我到目前为止的代码。提前致谢。

Button(action: {
                    self.isPlayed.toggle()//toggles the boolean isPlayed
                    self.appear.toggle()//toggles the boolean appear


                }, label: {

                    //if the boolean of isPlayed and
                    if isPlayed && appear{
                        Image(systemName: "pause")
                            .font(Font.system(size: 30))

                    } else{
                        Image(systemName:"play")
                            .font(Font.system(size: 30))
                    }

                })
                Spacer()
                }

            if appear == true{

               WebView(request: URLRequest(url: URL(string: "http:www.\(websites[counter])?playsinline=1")!))
                   .frame(width:300, height:300)
                .padding()
                .opacity(1)

            }

        }

        }
}

struct WebView: UIViewRepresentable {
    let request: URLRequest//pass the website to webkit

    func makeUIView(context: Context) -> WKWebView {
        return WKWebView()

    }
    func updateUIView(_ uiView: WKWebView, context: Context) {
        uiView.load(request)

    }
}
Run Code Online (Sandbox Code Playgroud)

Asp*_*eri 7

可以通过WKWebViewConfiguration以下方式完成

func makeUIView(context: Context) -> WKWebView {
    let configuration = WKWebViewConfiguration()
    configuration.allowsInlineMediaPlayback = true

    let webView = WKWebView(frame: .zero, configuration: configuration)
    return webView
}
Run Code Online (Sandbox Code Playgroud)