gpt4 book ai didi

ios - 带有快速下载按钮的视频播放器

转载 作者:搜寻专家 更新时间:2023-11-01 05:43:40 24 4
gpt4 key购买 nike

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

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()

最佳答案

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

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

关于ios - 带有快速下载按钮的视频播放器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49714560/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com