当 NSAllowsArbitraryLoadsInWebContent 为真时无法使用 AVPlayer 播放视频

Cannot play videos with AVPlayer when NSAllowsArbitraryLoadsInWebContent is true

我有一个非常小的 AVPlayer 应用程序。我正在使用以下代码播放视频:

if let urlToPlay = URL(string: "http://some_video.m3u8") {

    self.player = AVPlayer.init(url: urlToPlay)
    self.player!.play()
}

我还在文件 "Info.plist" 中添加了一些行,以便能够播放具有 HTTP 架构的视频:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

而且效果很好。但是后来我发现了一个叫NSAllowsArbitraryLoadsInWebContent的flag。我的应用程序中没有任何 WKWebViewUIWebView 实例,因此我认为使用此标志不会导致我的应用程序出现任何崩溃或错误。现在我的文件 "Info.plist" 有这个片段:

<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
    <key>NSAllowsArbitraryLoadsInWebContent</key>
    <true/>
</dict>

播放器不工作。有人可以向我解释为什么会这样吗?

对于通过 AVFoundation 加载媒体添加密钥 NSAllowsArbitraryLoadsForMedia 并将其设置为 true。

根据文档:

In iOS 10 and later, and macOS 10.12 and later, the value of this key is ignored—resulting in an effective value for this key of its default value of NO—if any of the following keys are present in your app’s Info.plist file:

NSAllowsArbitraryLoadsForMedia, NSAllowsArbitraryLoadsInWebContent, NSAllowsLocalNetworking

请注意,在生产环境中不鼓励设置任意负载,并且这些标志会触发 AppStore 审查并需要证明这样做的理由。

如需进一步阅读,请访问 ATS Documentation

Also Create a AVPlayerViewController object to play the Video.

 let video = AVPlayer(url: URL(fileURLWithPath:path))
 let videoPlayer = AVPlayerViewController()
 videoPlayer.player = video
 present(videoPlayer, animated:true, completion: {
        video.play()
   })

Hope this will helps for you.