gpt4 book ai didi

swift - 下载音频文件

转载 作者:行者123 更新时间:2023-11-28 05:52:29 25 4
gpt4 key购买 nike

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

当点击下载按钮时会出现一个圆形的进度条。

当我返回 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)
}
}
}

}

}

最佳答案

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

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

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

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

关于swift - 下载音频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52424257/

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