如何解决即使文件存在且可访问,也无法播放音频网址
我在我的应用程序中编写了一个下载函数,它从一个 url 下载一个 .mp3 文件并将其保存到本地的应用程序支持目录中。
func urlSession(_ session: URLSession,downloadTask: URLSessionDownloadTask,didFinishDownloadingTo location: URL) {
if let url = downloadTask.originalRequest?.url {
let destinationURL = moveFileToSupportDirectory(source: location,fileName: url.lastPathComponent,subDir: "MyApp")
onDownloadComplete?(destinationURL,nil)
}
}
func moveFileToSupportDirectory(source: URL,fileName: String,subDir: String) -> URL? {
do {
let manager = FileManager.default
let directoryURL = try manager.url(for: .applicationSupportDirectory,in: .userDomainMask,appropriateFor: nil,create: true).appendingPathComponent(subDir)
let destination = directoryURL.appendingPathComponent(fileName)
do {
try FileManager.default.createDirectory (at: directoryURL,withIntermediateDirectories: true,attributes: nil)
} catch {
print("Error creating directory: \(error)")
return nil
}
if FileManager().fileExists(atPath: destination.path) {
print("File already exists [\(destination.path)]")
return destination
}
else {
print("Move from \(source.path) to destination \(destination.path)...")
try manager.moveItem(at: source,to: destination)
return destination
}
} catch {
printTrace("\(error)")
return nil
}
}
之后,当用户按下“播放”按钮时,应用将使用 fileName 检索保存的 URL 并在本地播放。
获取网址的功能:
func getSavedURL(of name: String) -> URL? {
do {
let manager = FileManager.default
let directoryURL = try manager.url(for: .applicationSupportDirectory,create: true).appendingPathComponent("MyApp")
let destination = directoryURL.appendingPathComponent(name)
if FileManager().fileExists(atPath: destination.path) {
print("File exists [\(destination.path)]")
let isReachable = try destination.checkResourceIsReachable()
if isReachable {
print("File is reachable \(destination.absoluteString)")
return destination
} else {
print("File is unreachable")
return nil
}
}
else {
print("File \(name) doesn't exist")
return nil
}
} catch {
print("\(error)")
return nil
}
}
问题是,当我在同一个版本中下载并播放它时,一切正常。但是,如果我开始另一个构建并播放之前保存的文件,音频播放器将抛出以下错误:
ExtAudioFile.cpp:193:Open: about to throw 'wht?': open audio file
[avae] AVAEInternal.h:109 [AVAudioFile.mm:134:AVAudioFileImpl: (ExtAudioFileOpenURL((CFURLRef)fileURL,&_extAudioFile)): error 2003334207
从这个线程来看,错误似乎与音频文件为零有关:
OSStatus error 2003334207 when using AVAudioPlayer
但是在检索 URL 时,它已经通过了所有检查(即存在且可访问)并返回一个非零 URL。音频文件存在并且可以访问,为什么不能正常播放?
是否与这个沙盒问题有关?
Document directory path change when rebuild application。
但是我使用相对方法检索URL,我认为这不应该发生在我的情况下。
附言我使用 SwiftAudioPlayer 在本地播放我的音频文件。里面好像用了AudioEngine。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。