Swift 我正在下载一个音频文件并将其保存在目录中,但我无法';我不玩它。(OSStatus错误2003334207。)
这是我正在下载的代码Swift 我正在下载一个音频文件并将其保存在目录中,但我无法';我不玩它。(OSStatus错误2003334207。),swift,avplayer,osstatus,Swift,Avplayer,Osstatus,这是我正在下载的代码 if let audioUrl = URL(string: UrlTextfield.text!) { // then lets create your document folder url let documentsDirectoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! //
if let audioUrl = URL(string: UrlTextfield.text!) {
// then lets create your document folder url
let documentsDirectoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
// let documentsDirectoryURL = URL(fileURLWithPath: Bundle.main.resourcePath!)
// 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")
// if the file doesn't exist
} else {
// you can use NSURLSession.sharedSession to download the data asynchronously
URLSession.shared.downloadTask(with: audioUrl) { location, response, error in
guard let location = location, error == nil else { return }
do {
// after downloading your file you need to move it to your destination url
try FileManager.default.moveItem(at: location, to: documentsDirectoryURL)
print("File moved to documents folder")
self.gettingSongNames()
} catch {
print("Error is \(error.localizedDescription)")
}
}.resume()
在这里,我在tableview单元中播放它
let audioPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let audioPath1 = audioPath.appendingPathComponent(songs[indexPath.row])
try audioPlayer = AVAudioPlayer(contentsOf: audioPath1)
audioPlayer.play()
thisSong = indexPath.row
audioStaff = true
let audioFileURL = NSURL(fileURLWithPath: destinationUrl.path)
do {
audioFile = try AVAudioPlayer(contentsOf: audioFileURL as URL )
audioFile.enableRate = true
audioFile.prepareToPlay()
audioFile.play()
} catch let err as NSError {
print(err.debugDescription)
}