保存 .mp3 以快速离线播放
我正在尝试保存从 url 下载的 .mp3,显然我保存了该文件,但在播放时,仅使用 deebug 我就可以听它 5 秒钟。
func saveAudioLocally(fileName: String, name: String, guideNumber: String){
if let audioUrl = URL(string: fileName) {
// 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("\(name)_\(guideNumber)")
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: destinationUrl)
print("File moved to documents folder")
print(destinationUrl)
} catch {
print(error)
}
}.resume()
}
}
}
播放我正在使用的音频
let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
url = documentsDirectory.appendingPathComponent("\(self.track.name)_\(self.track.idTrackNavigation.guideNumber)")
do {
let audioPlayer = try AVAudioPlayer(contentsOf: url!)
audioPlayer.prepareToPlay()
audioPlayer.play()
} catch let error {
print(error.localizedDescription)
}
I am trying to save an .mp3 that I download from a url, apparently I save the file but at the moment of playing in, just with deebug I could listen to it for 5 seconds.
func saveAudioLocally(fileName: String, name: String, guideNumber: String){
if let audioUrl = URL(string: fileName) {
// 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("\(name)_\(guideNumber)")
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: destinationUrl)
print("File moved to documents folder")
print(destinationUrl)
} catch {
print(error)
}
}.resume()
}
}
}
To play the audio I'm using
let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
url = documentsDirectory.appendingPathComponent("\(self.track.name)_\(self.track.idTrackNavigation.guideNumber)")
do {
let audioPlayer = try AVAudioPlayer(contentsOf: url!)
audioPlayer.prepareToPlay()
audioPlayer.play()
} catch let error {
print(error.localizedDescription)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
让我知道这是否适合您。
用它来检查文件是否存在。
使用:
Let me know if this works for you.
Use this to check if the file exists.
Use of: