swift - 将 [URL] 提取到 URL
问题描述
我正在尝试提取 Firebase 数据库 url 引用的音频文件。我创建的功能可以下载[URL]
但 AVAudioPlayer 需要URL
它可以在本地播放。总体目标是流式传输音频文件。我的问题是有什么方法可以将此 url 从数组中导出为常规的 url 字符串?这篇SO 帖子是我能想到的最接近我的问题但并没有真正帮助的帖子。下面是 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)
}
}
}
}
}
解决方案
通过使用FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
您将获得所有不是播放特定音频文件或保存它所需的 url 。
因此,您可能需要通过执行以下操作要求 FileManager 生成文件所在的 url:
let localURL = try! FileManager.default.url(for: .documentDirectory,
in: .userDomainMask,
appropriateFor: nil,
create: false)
.appendingPathComponent("yourFileName.mp3")
推荐阅读
- mysql - 在 MySQL 中实现 where 条件 varchar 字段
- c++ - 位移行为
- oracle - 在oracle中将部分重复的行合并为一个
- ios - 如果在设备平坦 Swift 时加载 VC,则设备的方向会失败
- flutter - Flutter Column with parent Animated 高度溢出问题
- spacy - spacy tokenizer:有没有办法使用正则表达式作为 update_exc 的自定义异常中的键
- c# - Roslyn 代码分析拼写检查器无法识别内插字符串文字中的代码标识符
- xml - 无法在 XSLT 中使用子节点的值
- sql - Oracle 报错“包名和模式名必须与编辑对象匹配”
- sylius - 添加新实体 Sylius