Search code examples
iosswift3alamofireavplayernsdata

Download Video using Alamofire and then Play on AV player


I have Used Following Code to Download Video from the URL. It is working fine for Downloading it the Video.

func downloadVideo(){

    Alamofire.request("https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4").downloadProgress(closure:{ (progress) in
    print(progress.fractionCompleted)
    self.progressView.progress = Float(progress.fractionCompleted)

    }).responseData{ (response) in
       print(response.result)
       print(response.result.value!)
       print(response.result.description)

        if let data = response.result.value {
            let obj = String(data: data, encoding: .utf8)
            let player = AVPlayer(url: URL(string: obj!)!)
            self.playerController.player = player
            self.addChildViewController(self.playerController)
            self.view.addSubview(self.playerController.view)
            self.playerController.view.frame = self.view.frame
            player.play()

        }

    }

But I want to Play this Video in AVPlayer

I have Found that May be Video is Downloaded as NSData. am in Right.? Can Anyone Help me How to Play this Video once it has Finished Downloading?

It Would be good if anyone Can suggest some another Code For downloading video with Progress Bar and then Playing it in Swift 3.0.

TIA.


Solution

  • Afters Hours of Searching for this stuff

    I came up With an easy and reliable way of Converting the NSData in URL so that it can be played by AVPlayer

    func downloadVideo(){
    
        Alamofire.request("https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4").downloadProgress(closure : { (progress) in
        print(progress.fractionCompleted)
        self.progressView.progress = Float(progress.fractionCompleted)
        }).responseData{ (response) in
           print(response)
           print(response.result.value!)
           print(response.result.description)
            if let data = response.result.value {
    
                let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
                let videoURL = documentsURL.appendingPathComponent("video.mp4")
                do {
                    try data.write(to: videoURL)
                    } catch {
                    print("Something went wrong!")
                }
                print(videoURL)
                let player = AVPlayer(url: videoURL as URL)
                self.playerController.player = player
                self.addChildViewController(self.playerController)
                self.view.addSubview(self.playerController.view)
                self.playerController.view.frame = self.view.frame
                player.play()
            }
        }
    }
    

    You Can Use this Following Piece of Code to convert that

    if let data = response.result.value {
    
                let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
                let videoURL = documentsURL.appendingPathComponent("video.mp4")
                do {
                    try data.write(to: videoURL) // Pass this videoURL to AVPlayer after Downloading Video it will be played.
                    } catch {
                    print("Something went wrong!")
                }
                print(videoURL)
    

    As per the Request, Here is the Source Code of the full implementation.

    Download Source Code

    P.S:- This was made thinking of a demo. Many of you will argue that this code can be optimised and can be done better, but yes this is an demo and you can pick some of the part or whole depending upon your Requirement