我正在尝试提取Firebase数据库URL引用的音频文件。我创建的功能可以下载[URL]
,但AVAudioPlayer需要一个URL
才能从本地播放。总体目标是流式传输音频文件。我的问题是有什么方法可以将这个网址从阵列中导出到常规网址字符串中?这个SO post是我所能解决的最接近我的问题的方法,但实际上并没有帮助。以下是JSON路径的代码段和屏幕截图。非常感谢您的帮助和指导!
func fetchAudio(asset: AVURLAsset){
let storageRef = Storage.storage().reference() //Referencing database
var node : Post? //References the Post model
let remoteAudioURL = FetchAudio.shared.storageRef.reference(forURL: (node?.audioUrl)!) //Referencing the remote audio file by way of it's respective Post
let localURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask) //Instantiate the local destination for the downloaded audio file
remoteAudioURL.getData(maxSize: 10 * 1024 * 1024) { (data, error) in
if let error = error {
print (error.localizedDescription)
} else {
if let d = data {
do {
try d.write(to: localURL) //Error: Cannot convert value of type '[URL]' to expected argument type 'URL'
self.player = AVAudioPlayer(contentsOf: localURL) //Error: Cannot convert value of type '[URL]' to expected argument type 'URL'
self.player?.play()
} catch {
print(error)
}
}
}
}
}
答案 0 :(得分:1)
通过使用FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
您将获得所有的URL,而这些
因此,您可能需要执行以下操作,要求FileManager生成文件所在的网址:
let localURL = try! FileManager.default.url(for: .documentDirectory,
in: .userDomainMask,
appropriateFor: nil,
create: false)
.appendingPathComponent("yourFileName.mp3")