问题
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