10

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 ?

Ali Ihsan URAL
  • 1,894
  • 1
  • 20
  • 43
  • Can you please share If videURL returns a valid url? – Emre Önder Nov 20 '18 at 08:52
  • It is vaild. When you try to open with browser this url . The video is shown and played – Ali Ihsan URAL Nov 20 '18 at 08:56
  • can you say what fixed it? – ironRoei Jan 21 '19 at 13:00
  • I started getting this when started to use urlAsset.resourceLoader.preloadsEligibleContentKeys = true with streaming CMAF playlist. However i realized it only occurred on SIMULATOR, not device. For me it had nothing to do with AVPlayer as this was happening on a screen that didn't even instantiate a movie player yet. – Ryan Romanchuk Feb 05 '19 at 23:52

4 Answers4

0

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.

gulyashki
  • 449
  • 3
  • 5
0

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>"]])

NavinDev
  • 995
  • 1
  • 7
  • 8
0

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

0

NSErrorFailingURLKey=http://b...a.mp4 The url with "http" prefix always fails irrespective of what ATS you have. please visit my answer here

SaadurRehman
  • 622
  • 8
  • 20