Swift 下载音频文件
我有一个带有多个控制器的音频应用程序,VCMain控制器,其中所有书籍和VCBook控制器都显示在该书的周围,其中有下载音频文件的功能(该书可以有许多文件) 单击下载按钮时,会出现一个圆形进度条 如何做到这一点,当我返回VCMain并返回下载的书籍时,我拥有进度条设置所需的所有数据(哪些书籍仍在下载或正在下载)。我可以同时下载几本书Swift 下载音频文件,swift,download,progress-bar,nsurlsessiondownloadtask,urlsession,Swift,Download,Progress Bar,Nsurlsessiondownloadtask,Urlsession,我有一个带有多个控制器的音频应用程序,VCMain控制器,其中所有书籍和VCBook控制器都显示在该书的周围,其中有下载音频文件的功能(该书可以有许多文件) 单击下载按钮时,会出现一个圆形进度条 如何做到这一点,当我返回VCMain并返回下载的书籍时,我拥有进度条设置所需的所有数据(哪些书籍仍在下载或正在下载)。我可以同时下载几本书 var progressIndicatorView:CircularProgressView! lazy var sizeBytes:Int64 = 0 lazy
var progressIndicatorView:CircularProgressView!
lazy var sizeBytes:Int64 = 0
lazy var dowloandedSizeBytes:Int64 = 0
@objc func progressBar(){
DispatchQueue.main.async {
self.progressIndicatorView = CircularProgressView(frame: CGRect(x: 5, y: -20, width: 50, height: 50))
self.view.addSubview(self.progressIndicatorView)
self.download.isEnabled = false
self.download.isHidden = true
}
}
@objc func downloadBook(){
progressBar()
for i in UrlName{
Func.getDownloadSize(url: URL(string: i)!, completion: { [weak self] (size, error) in
self!.sizeBytes += size
})
if let audioUrl = URL(string: i) {
// create your document folder url
let documentsUrl = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
// your destination file url
let destination = documentsUrl.appendingPathComponent(audioUrl.lastPathComponent)
// check if it exists before downloading it
if FileManager().fileExists(atPath: destination.path) {
print("The file already exists at path")
bookMarkLoad()
Func.getDownloadSize(url: URL(string: i)!, completion: { [weak self] (size, error) in
self!.dowloandedSizeBytes += size
})
} else {
// if the file doesn't exist
// just download the data from your url
var url = URLRequest(url: audioUrl)
url.timeoutInterval = Double.infinity
let config = URLSessionConfiguration.background(withIdentifier: i)
let session = URLSession(configuration: config, delegate: self, delegateQueue: nil)
let task = session.downloadTask(with: url)
task.taskDescription = String(format:"%@" , i )
task.resume()
registerBackgroundTask()
// let _ = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(backgraundState), userInfo: nil, repeats: true)
}
}
}
}
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
if UrlName.contains(downloadTask.taskDescription!){
if totalBytesExpectedToWrite > 0 {
dowloandedSizeBytes += bytesWritten
let progress = Float(dowloandedSizeBytes) / Float(sizeBytes)
// print("Progress \(downloadTask) \(progress)")
DispatchQueue.main.async {
if self.progressIndicatorView != nil {
self.progressIndicatorView.progress = CGFloat(progress)
}
}
}
}
}
这是一个相当广泛的问题,但我建议首先将与下载相关的所有逻辑移动到一个单独的服务。将此类逻辑远离视图/视图控制器被认为是一种良好的做法 此服务将负责处理下载,并可以通知应用程序的其他部分(如ViewController)进度,例如使用通知 然后,MainVC可以从服务请求状态信息并更新视图,例如在
viewwillisped(u:)
方法中
有关如何开始构建应用程序和服务的一些示例,请访问: