I try to play a video from url string. But I have get some error as question title.
I try this code in below. videoPath is a url string.
let videoURL = URL(string: videoPath)
let player = AVPlayer(url: videoURL!)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.present(playerViewController, animated: true) {
playerViewController.player!.play()
}
Below is error log :
load failed with error Error Domain=NSURLErrorDomain Code=-999 "cancelled" UserInfo={NSErrorFailingURLStringKey= http://b...a.mp4 , NSErrorFailingURLKey= http://b...a.mp4 , _NSURLErrorRelatedURLSessionTaskErrorKey=( "LocalDataTask <841B2FFA-479B-4E5A-9BD3-D9207EAA0D32>.<2>" ), _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <841B2FFA-479B-4E5A-9BD3-D9207EAA0D32>.<2>, NSLocalizedDescription=cancelled} [-999]
I set the info.plist --
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
<key>NSExceptionDomains</key>
<dict>
<key>www.example.com</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSExceptionMinimumTLSVersion</key>
<string>TLSv1.1</string>
<key>NSIncludesSubdomains</key>
<true/>
</dict>
</dict>
</dict>
NOTE: The problem is occured with 10 minutes longer videos. Is it a normal ?
Are you trying to present the AVPlayerViewController
inside viewDidLoad
? If so you could try presenting it after the view controller's view is added to the window hierarchy - viewDidAppear
for example. Keep in mind that viewDidAppear
will be called when you navigate back to the controller and the modal presentation will be triggered again.
Does the URL require cookies to be set ? I faced the same issue with missing cookies. You can check by trying to open the url in an incognito-window. If it still plays fine then perhaps you can debug this by -
Creating an AVURLAsset
object with the URL eg -
AVURLAsset(url: <URL>, options:[])
and set the resourceLoader
delegate to self. Like urlAsset?.resourceLoader.setDelegate(self, queue: .main)
and implement the functions
func resourceLoader(_ resourceLoader: AVAssetResourceLoader, shouldWaitForLoadingOfRequestedResource loadingRequest: AVAssetResourceLoadingRequest) -> Bool {
return true
}
func resourceLoader(_ resourceLoader: AVAssetResourceLoader, shouldWaitForRenewalOfRequestedResource renewalRequest: AVAssetResourceRenewalRequest) -> Bool {
return true
}
func resourceLoader(_ resourceLoader: AVAssetResourceLoader, didCancel loadingRequest: AVAssetResourceLoadingRequest) {
}
func resourceLoader(_ resourceLoader: AVAssetResourceLoader, shouldWaitForResponseTo authenticationChallenge: URLAuthenticationChallenge) -> Bool {
return true
}
func resourceLoader(_ resourceLoader: AVAssetResourceLoader, didCancel authenticationChallenge: URLAuthenticationChallenge) {
}
But if it does require cookies - Set them in the AVURLAsset
object eg let asset = AVURLAsset(url: videoURL, options: ["AVURLAssetHTTPHeaderFieldsKey": ["Cookie": "<YOUR TOKEN>"]])
The reason could be wrong metadata in the video. Take a look at this thread which I answered: AVPlayer HLS live stream IOS
The transcoded video needs to have profile baseline
in order to be played in AVPlayer. Take look at the ffmpeg transcoding command for details:
https://gist.github.com/chung-nguyen/d88e73e3cc8788878f5ffb8c232b4729
NSErrorFailingURLKey= http://b...a.mp4 The url with "http" prefix always fails irrespective of what ATS you have. please visit my answer here
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.