我是编程和swift的新手,我从服务器下载音频文件时遇到问题,这是我的代码:
var player: AVAudioPlayer?
func downloadSong()
{
let destination = DownloadRequest.suggestedDownloadDestination(for: .documentDirectory)
let url = "https://link for DWNL music"
print("DESTINATION \(destination)")
Alamofire.download(
url,
method: .get,
parameters: nil,
encoding: JSONEncoding.default,
headers: nil,
to: destination).downloadProgress(closure: { (progress) in
//progress closure
}).response(completionHandler: { (DefaultDownloadResponse) in
let destinationUrl = DefaultDownloadResponse.destinationURL
print("Downloaded file to \(DefaultDownloadResponse.destinationURL!)")
do {
self.player = try AVAudioPlayer(contentsOf: destinationUrl!)
guard let player = self.player else { return }
player.delegate = self
player.prepareToPlay()
player.play()
} catch let error {
print(error.localizedDescription)
}
})
}
它通过了所有代码,但没有播放任何内容...请告诉我这里我做错了什么。我对保存文件有疑问,我认为错误就在那里。谢谢!