从 firebase 存储获取 URL 并将其设置为 AVAudioPlayer 总是会抛出错误 (swift xcode)

Getting URL from firebase storage and setting it to an AVAudioPlayer always throws error (swift xcode)

我正在尝试从 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!)
                completion(self.audioPlayer)
                print("THIS HAS LOADED")
            }
            catch
            {
                print("COULD NOT ASSIGN URL") /**alert**/
                //ERROR here that keeps on happening :(
            }
        }
    }

我知道它是存储中的正确文件,并且由于要检查我的打印语句而获取 url 没有错误,但是,由于某种原因,设置此 URL到我的AVAudioPlayer的内容,audioPlayer

而不是:

try self.audioPlayer = AVAudioPlayer(contentsOf: audioURL!)

尝试使用这个(注意尝试位置):

 do {
    self.audioPlayer = try AVAudioPlayer(contentsOf: audioURL!)
    completion(self.audioPlayer)
} catch {
    print("===> " + error.localizedDescription)
}

您当然也可以使用 AVPlayer(没有 do/catch)

@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
        {
            print("COULD NOT ASSIGN URL - Error = /(error)") /**alert**/
            //ERROR here that keeps on happening :(
        }
    }
}