我試圖從Firebase存盤中下載一個URL,并將其保存到AVAudioPlayer中
。func loadAudio(post: CompPost, completion: @escaping (AVAudioPlayer) ->())
{
let audioRef = Storage.storage()。 reference().child("cPosts").child(post.uid).child("Audio").child(post.mp3)
audioRef.downloadURL
{ (url, error) in.
print("mp3filename: " post.mp3)
guard error == nil else
{
print(error!.localizedDescription)
return /**alert**/ }
let audioURL = url
print("url:" audioURL!.absoluteString)
do.
{
try self.audioPlayer = AVAudioPlayer( contentsOf: audioURL! )
完成(self.audioPlayer)
print("this HAS LOADED")
}
catch("this has LOADED")
{
print("COULD NOT ASSIGN URL") /**alert **/
//ERROR here that keeps on happening :()
}
}
}
我知道這是一個正確的存盤檔案,而且因為我的列印陳述句的檢查,沒有錯誤得到的url,然而,由于某種原因,有一個問題,設定這個URL到我的AVAudioPlayer的內容,audioPlayer
uj5u.com熱心網友回復:
@jnpdx的意思是(我認為是)--這樣你可以讓iOS直接從遠程URL流,而不用自己處理下載檔案
func loadAudio(post: CompPost, completion: @escaping (AVAudioPlayer) ->())
{
let audioRef = Storage.storage()。 reference().child("cPosts").child(post.uid).child("Audio").child(post.mp3)
audioRef.downloadURL
{ (url, error) in.
print("mp3filename: " post.mp3)
guard error == nil else
{
print(error!.localizedDescription)
return /**alert**/ }
let audioURL = url
print("url:" audioURL!.absoluteString)
do.
{
**try self. audioPlayer = AVPlayer(url: audioURL! )**
completion(self.audioPlayer)
print("this HAS LOADED")
}
catch("this has LOADED")
{
print("COULD NOT ASSIGN URL - Error = /(error)") /**alert**/
//error here that keeps on happening :()
}
}
}
uj5u.com熱心網友回復:
代替:
try self. audioPlayer = AVAudioPlayer(contentsOf: audioURL! )
嘗試使用這個(注意嘗試的位置):
do {
self.audioPlayer = try AVAudioPlayer( contentsOf: audioURL! )
完成(self.audioPlayer)
} catch {
print("===> " error.localizedDescription)
}
你當然也可以使用AVPlayer(沒有do/catch)
。轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/319546.html
標籤:
上一篇:如何忽略無效的URL引數?
下一篇:React路由不匹配查詢字串
