视频播放器与swift中的下载按钮

时间:2018-04-08 04:56:54

标签: ios swift download swift4 video-player

我有一个视频播放器,我想添加一个下载按钮。 如何将此按钮添加到我的视频播放器或创建一个按钮来下载该视频。 坦克。 我的代码是:

let videoURL = URL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
let player = AVPlayer(url: videoURL!)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.present(playerViewController, animated: true) {
playerViewController.player!.play()

1 个答案:

答案 0 :(得分:-1)

请检查以下代码,下载是在后台线程中进行的,因此不会影响用户界面。

class func downloadVideo(videoImageUrl:String)
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {

        let url=NSURL(string: videoImageUrl)
        let urlData=NSData(contentsOfURL: url!)

        if((urlData) != nil)
        {
            let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]

            let fileName = videoImageUrl.lastPathComponent //.stringByDeletingPathExtension

            let filePath="\(documentsPath)/\(fileName)"

            //saving is done on main thread

            dispatch_async(dispatch_get_main_queue(), { () -> Void in

                 urlData?.writeToFile(filePath, atomically: true)
            })

        }
    })

}

用于在按钮目标中调用上述功能。

Self.downloadVideo("https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")