停止视频自动播放 Swift UI

huangapple go评论135阅读模式
英文:

stop video from auto playing swift UI

问题

  1. import SwiftUI
  2. import WebKit
  3. struct YouTubeView: UIViewRepresentable {
  4. let videoId: String
  5. func makeUIView(context: Context) -> WKWebView {
  6. let webView = WKWebView()
  7. webView.configuration.allowsInlineMediaPlayback = true
  8. webView.configuration.mediaTypesRequiringUserActionForPlayback = []
  9. return webView
  10. }
  11. func updateUIView(_ uiView: WKWebView, context: Context) {
  12. guard let demoURL = URL(string: "https://s1.fwmrm.net/m/1/169843/20/89977492/AIDP7266000H_ENT_MEZZ_HULU_8166176_578.mp4") else { return }
  13. uiView.scrollView.isScrollEnabled = false
  14. uiView.load(URLRequest(url: demoURL))
  15. }
  16. }
  17. struct VideoPlayerView: View {
  18. var ids = "hzls6ZUHCYM"
  19. var body: some View {
  20. ZStack {
  21. ScrollView(showsIndicators: false) {
  22. VStack {
  23. YouTubeView(videoId: ids)
  24. .frame(width: 300, height: 175)
  25. }
  26. }
  27. }
  28. }
  29. }
  30. //modifier correct YouTube struct
  31. struct YouTubeView: UIViewRepresentable {
  32. var videoID: String
  33. func makeUIView(context: Context) -> WKWebView {
  34. let webConfiguration = WKWebViewConfiguration()
  35. webConfiguration.allowsInlineMediaPlayback = true
  36. return WKWebView(frame: .zero, configuration: webConfiguration)
  37. }
  38. func updateUIView(_ uiView: WKWebView, context: Context) {
  39. let embedHTML = """
  40. <!DOCTYPE html>
  41. <html>
  42. <body>
  43. <iframe width="800" height="550" src="https://www.youtube.com/embed/\(videoID)" frameborder="0" allowfullscreen></iframe>
  44. </body>
  45. </html>
  46. """
  47. uiView.loadHTMLString(embedHTML, baseURL: nil)
  48. }
  49. }
英文:

Hello I a have some code that loads a web video into a swift ui view and allows it to be played. Currently the video auto plays when it loads in and I would like to stop this behavior. Additionally the video can only be viewed in full screen mode and I can't figure out how to make it so that it can play while not being in full screen. I also get the warning "Error acquiring assertion: <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 did not work. If anyone knows how to get these 2 pieces of functionality working I would appreciate it, thanks.

  1. import SwiftUI
  2. import WebKit
  3. struct YouTubeView: UIViewRepresentable {
  4. let videoId: String
  5. func makeUIView(context: Context) -&gt; WKWebView {
  6. let webView = WKWebView()
  7. webView.configuration.allowsInlineMediaPlayback = true
  8. webView.configuration.mediaTypesRequiringUserActionForPlayback = []
  9. return webView
  10. }
  11. func updateUIView(_ uiView: WKWebView, context: Context) {
  12. guard let demoURL = URL(string: &quot;https://s1.fwmrm.net/m/1/169843/20/89977492/AIDP7266000H_ENT_MEZZ_HULU_8166176_578.mp4&quot;) else { return }
  13. uiView.scrollView.isScrollEnabled = false
  14. uiView.load(URLRequest(url: demoURL))
  15. }
  16. }
  17. struct VideoPlayerView: View {
  18. var ids = &quot;hzls6ZUHCYM&quot;
  19. var body: some View {
  20. ZStack {
  21. ScrollView(showsIndicators: false) {
  22. VStack {
  23. YouTubeView(videoId: ids)
  24. .frame(width: 300, height: 175)
  25. }
  26. }
  27. }
  28. }
  29. }
  30. //modifier correct YouTube struct
  31. struct YouTubeView: UIViewRepresentable {
  32. var videoID: String
  33. func makeUIView(context: Context) -&gt; WKWebView {
  34. let webConfiguration = WKWebViewConfiguration()
  35. webConfiguration.allowsInlineMediaPlayback = true
  36. return WKWebView(frame: .zero, configuration: webConfiguration)
  37. }
  38. func updateUIView(_ uiView: WKWebView, context: Context) {
  39. let embedHTML = &quot;&quot;&quot;
  40. &lt;!DOCTYPE html&gt;
  41. &lt;html&gt;
  42. &lt;body&gt;
  43. &lt;iframe width=&quot;800&quot; height=&quot;550&quot; src=&quot;https://www.youtube.com/embed/\(videoID)&quot; frameborder=&quot;0&quot; allowfullscreen&gt;&lt;/iframe&gt;
  44. &lt;/body&gt;
  45. &lt;/html&gt;
  46. &quot;&quot;&quot;
  47. uiView.loadHTMLString(embedHTML, baseURL: nil)
  48. }
  49. }

答案1

得分: 2

尝试将视频嵌入到 HTML 中,而不是直接使用 mp4 URL。在 video 标签中设置 playsinline 参数。在实例化 WKWebView 对象之前,还要设置 allowsInlineMediaPlayback 参数。

至于权限错误,其他 SO 回答 表明可以简单忽略。

  1. let html = """
  2. <video
  3. src="https://s1.fwmrm.net/m/1/169843/20/89977492/AIDP7266000H_ENT_MEZZ_HULU_8166176_578.mp4"
  4. width="640" height="480"
  5. controls
  6. playsinline="true">
  7. """
  8. struct YouTubeView: UIViewRepresentable {
  9. func makeUIView(context: Context) -> WKWebView {
  10. let webConfiguration = WKWebViewConfiguration()
  11. webConfiguration.allowsInlineMediaPlayback = true //首先设置配置
  12. return WKWebView(frame: .zero, configuration: webConfiguration)
  13. }
  14. func updateUIView(_ uiView: WKWebView, context: Context) {
  15. uiView.loadHTMLString(html, baseURL: nil) //加载嵌入在 HTML 中的视频
  16. }
  17. }
英文:

try loading your video embedded within html rather than directly to an mp4 url. set the playsinline parameter for the video tag. also set allowsInlineMediaPlayback prior to instantiating the WKWebView object.

as for the entitlement error, other SO answers seem to indicate that this can simply be ignored.

  1. let html = &quot;&quot;&quot;
  2. &lt;video
  3. src=&quot;https://s1.fwmrm.net/m/1/169843/20/89977492/AIDP7266000H_ENT_MEZZ_HULU_8166176_578.mp4&quot;
  4. width=&quot;640&quot; height=&quot;480&quot;
  5. controls
  6. playsinline=&quot;true&quot;&gt;
  7. &quot;&quot;&quot;
  8. struct YouTubeView: UIViewRepresentable {
  9. func makeUIView(context: Context) -&gt; WKWebView {
  10. let webConfiguration = WKWebViewConfiguration()
  11. webConfiguration.allowsInlineMediaPlayback = true //set up config first
  12. return WKWebView(frame: .zero, configuration: webConfiguration)
  13. }
  14. func updateUIView(_ uiView: WKWebView, context: Context) {
  15. uiView.loadHTMLString(html, baseURL: nil) //load video embedded inside html
  16. }
  17. }

huangapple
  • 本文由 发表于 2023年5月26日 13:13:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76337824.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定