下载音频文件

Downloading audio files

我有一个带有多个控制器的音频应用程序,其中显示所有书籍的 VCMain 控制器和关于书籍的 VCBook 控制器,其中具有下载音频文件的功能(书籍可以有很多文件)。

点击下载按钮时出现圆形进度条。

如何做到当我返回 VCMain 并返回已下载的图书时,我拥有进度条设置的所有必要数据(哪些图书仍在下载或正在下载)。我可以同时下载几本书

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

    }

}

这是一个相当宽泛的问题,但我建议首先将与下载相关的所有逻辑移至单独的服务。将此类逻辑远离您的 views/viewcontrollers 被认为是一种很好的做法。

此服务将负责处理下载并可以将进度通知应用程序的其他部分(例如视图控制器),例如通过使用通知。

MainVC 然后可以从服务请求状态信息并更新视图,例如在 viewWillAppear(_:) 方法中。

有关如何开始构建您的应用程序和服务的一些示例,请访问: