Swift - AVPlayer load failed with error Error Domain=NSURLErrorDomain Code=-999 “cancelled”












1














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>









share|improve this question
























  • Can you please share If videURL returns a valid url?
    – Emre Önder
    Nov 20 '18 at 8:52










  • It is vaild. When you try to open with browser this url . The video is shown and played
    – codeByThey
    Nov 20 '18 at 8:56
















1














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>









share|improve this question
























  • Can you please share If videURL returns a valid url?
    – Emre Önder
    Nov 20 '18 at 8:52










  • It is vaild. When you try to open with browser this url . The video is shown and played
    – codeByThey
    Nov 20 '18 at 8:56














1












1








1







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>









share|improve this question















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>






ios swift avplayer audiotoolbox






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 11:12









Rengers

7,75512343




7,75512343










asked Nov 20 '18 at 8:42









codeByThey

730322




730322












  • Can you please share If videURL returns a valid url?
    – Emre Önder
    Nov 20 '18 at 8:52










  • It is vaild. When you try to open with browser this url . The video is shown and played
    – codeByThey
    Nov 20 '18 at 8:56


















  • Can you please share If videURL returns a valid url?
    – Emre Önder
    Nov 20 '18 at 8:52










  • It is vaild. When you try to open with browser this url . The video is shown and played
    – codeByThey
    Nov 20 '18 at 8:56
















Can you please share If videURL returns a valid url?
– Emre Önder
Nov 20 '18 at 8:52




Can you please share If videURL returns a valid url?
– Emre Önder
Nov 20 '18 at 8:52












It is vaild. When you try to open with browser this url . The video is shown and played
– codeByThey
Nov 20 '18 at 8:56




It is vaild. When you try to open with browser this url . The video is shown and played
– codeByThey
Nov 20 '18 at 8:56












2 Answers
2






active

oldest

votes


















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.






share|improve this answer





















  • It's in viewDidAppear
    – codeByThey
    Nov 20 '18 at 10:30



















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






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53389144%2fswift-avplayer-load-failed-with-error-error-domain-nsurlerrordomain-code-999%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    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.






    share|improve this answer





















    • It's in viewDidAppear
      – codeByThey
      Nov 20 '18 at 10:30
















    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.






    share|improve this answer





















    • It's in viewDidAppear
      – codeByThey
      Nov 20 '18 at 10:30














    0












    0








    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.






    share|improve this answer












    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.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 20 '18 at 9:58









    gulyashki

    35124




    35124












    • It's in viewDidAppear
      – codeByThey
      Nov 20 '18 at 10:30


















    • It's in viewDidAppear
      – codeByThey
      Nov 20 '18 at 10:30
















    It's in viewDidAppear
    – codeByThey
    Nov 20 '18 at 10:30




    It's in viewDidAppear
    – codeByThey
    Nov 20 '18 at 10:30













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






    share|improve this answer


























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






      share|improve this answer
























        0












        0








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






        share|improve this answer












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







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 '18 at 4:41









        NavinDev

        915178




        915178






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53389144%2fswift-avplayer-load-failed-with-error-error-domain-nsurlerrordomain-code-999%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            If I really need a card on my start hand, how many mulligans make sense? [duplicate]

            Alcedinidae

            Can an atomic nucleus contain both particles and antiparticles? [duplicate]