Cannot play videos with AVPlayer when NSAllowsArbitraryLoadsInWebContent is true

末鹿安然 提交于 2021-01-28 04:51:25

问题


I have a very small application with AVPlayer. I am using the following code to play a video:

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

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

I also added some lines into the file "Info.plist" to be able to play videos with HTTP schemas:

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

And it works fine. But later I found a flag called NSAllowsArbitraryLoadsInWebContent. I don't have any WKWebView or UIWebView instances in my application, so I supposed that using of this flag will not lead to any crashes or errors in my application. Now my file "Info.plist" has this fragment:

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

And player is not working. Could someone explain me why it happened?


回答1:


For media loading through AVFoundation Add the key NSAllowsArbitraryLoadsForMedia and set it to true.

According to the documentation:

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

Note that setting arbitrary loads are not encouraged in production and these flags triggers an AppStore review and requires justification for doing so.

For further reading visit ATS Documentation




回答2:


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.


来源:https://stackoverflow.com/questions/50506308/cannot-play-videos-with-avplayer-when-nsallowsarbitraryloadsinwebcontent-is-true

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!