簡體   English   中英

如何將從 Firebase 存儲接收的視頻字節設置為視頻播放器?

[英]How can I set the video bytes received from Firebase storage into a video player?

我試圖從 Firebase 存儲中檢索/加載視頻以將這些字節用於 iOS 播放器,但我失敗得很慘。 我從那里獲取正確的數據(我使用調試並獲取正確的字節,即 84611570)但我不能將它與 MPMoviePlayerController 或 AVPlayer 一起使用,因為兩者都使用 URL 方法。 我試圖提供來自服務器的路徑,但存儲沒有 https sintax 有一些 gs:// 或一些關於它的東西。 我需要一些關於某些方法的指導,因為我浪費了很多時間並且沒有繼續前進(加載 1%)。 我的代碼在下面供某人檢查。 關於如何轉換數據並放入播放器的任何想法?

   func downloadVideo(filename:String){
    let downloadVideoReference = videoReference.child(filename)

    let dowloadTask = downloadVideoReference.getData(maxSize: 1024 * 1024 * 1024 * 1024) { (data, erro) in
        if data != nil {

            print(data)
            print(type(of: data))

     }
    }

}

如果您想使用 HTTP URL 將數據從 Firebase 存儲傳遞給播放器,您需要生成一個下載 URL 下載 URL 是 URL,它向任何人(擁有該 URL)提供對數據的只讀訪問,因此您可以在播放器上設置它。

如果您不想使用下載 URL(因為這可能會與其他無需登錄即可訪問數據的用戶共享),您必須找到一個可以直接將數據傳遞到的播放器,而不是通過 URL。

這是我在我的 SwiftUI 項目中使用的代碼,以便執行此操作。 相信我,我理解花了幾個小時才讓它工作的挫敗感。

let videoRef = storage.reference().child("video/\(uid).mp4")

let view = UIViewController()

videoRef.getData(maxSize: 10 * 1024 * 1024) { data, error in

  if let error = error {

    print("ERROR: \(error)")
    print("Did not find video!")

  } else {

    let tmpFileURL = URL(fileURLWithPath:NSTemporaryDirectory()).appendingPathComponent("video").appendingPathExtension("mp4")
    do{
        try data!.write(to: tmpFileURL, options: [.atomic])
    }catch{
        print("error with video!")
    }

    let rect = CGRect(
        origin: CGPoint(x: 0, y: 0),
        size: self.myuiscreen.bounds.size
    )
    self.player1 = AVPlayer(url: tmpFileURL)

    let controller = AVPlayerLayer(player: self.player1)
    controller.player = self.player1
    self.player1!.isMuted = false

    do {
       try AVAudioSession.sharedInstance().setCategory(.playback)
    } catch(let error) {
        print(error.localizedDescription)
    }
    self.player1!.play()

    controller.videoGravity = AVLayerVideoGravity.resizeAspectFill
    controller.frame = rect
    view.view.layer.addSublayer(controller)

    //Where the video repeats
    NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: self.player1?.currentItem, queue: .main) { _ in
        self.player1!.seek(to: CMTime.zero)
        self.player1!.play()
    }
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM