gpt4 book ai didi

ios - 未调用 URLSession 委托(delegate)方法

转载 作者:行者123 更新时间:2023-11-29 06:02:13 25 4
gpt4 key购买 nike

我下载了这首歌,然后在按钮上播放。这部分的一切都在工作。但我想监控下载指示器并暂停和恢复下载。但未调用委托(delegate)方法。

class ViewController: UIViewController, URLSessionDownloadDelegate, URLSessionTaskDelegate,URLSessionDelegate  {
@IBOutlet weak var progressBar: UIProgressView!

let url = URL(string: "https://www.dropbox.com/s/s7zkka3at171wf8/Track01.mp3?raw=1")!

var audioPlayer : AVAudioPlayer!

var task: URLSessionDownloadTask!
let configuration1 = URLSessionConfiguration.default
var urlSession: URLSession!
let operationQueue1 = OperationQueue()

var url1 : URL!

// MARK: - Life CYcle
override func viewDidLoad() {
super.viewDidLoad()
}

// MARK: - IBActions
@IBAction func playAudio(_ sender: Any) {
do {
audioPlayer = try AVAudioPlayer(contentsOf: self.url1)
audioPlayer.play()
}
catch {
print("error")
}
}

@IBAction func pauseAudio(_ sender: Any) {}

@IBAction func downloadButton(_ sender: Any) {
guard let audioUrl = URL(string: "https://www.dropbox.com/s/s7zkka3at171wf8/Track01.mp3?raw=1") else {
return
}

// then lets create your document folder url
let documentsDirectoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!

// lets create your destination file url
let destinationUrl = documentsDirectoryURL.appendingPathComponent(audioUrl.lastPathComponent)
print(destinationUrl)

// to check if it exists before downloading it
if FileManager.default.fileExists(atPath: destinationUrl.path) {
print("The file already exists at path")
self.url1 = destinationUrl

// if the file doesn't exist
}
else {

print("0")
urlSession = URLSession(configuration: configuration1, delegate: self, delegateQueue: operationQueue1)

task = urlSession.downloadTask(with: audioUrl) { (location, response, error) in
guard
let location = location else {
return
}

do {
try FileManager.default.moveItem(at: location ,to : destinationUrl)
print("File moved to documents folder")
self.url1 = destinationUrl

}
catch let error as NSError{
print(error.localizedDescription)
}
print("1")
}

task.resume()
print("2")

}

print("3")
}

// MARK: -
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
print("fineshed download file")
}

func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
print(totalBytesWritten)
print(totalBytesExpectedToWrite)
}
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
print("All success")
}
}

最佳答案

试试这个

let configuration = URLSessionConfiguration.default
let session = URLSession(configuration: configuration, delegate: self, delegateQueue: OperationQueue.main)

关于ios - 未调用 URLSession 委托(delegate)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54531384/

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