【问题标题】:How can I set the video bytes received from Firebase storage into a video player?如何将从 Firebase 存储接收的视频字节设置到视频播放器中?
【发布时间】:2020-05-12 00:01:34
【问题描述】:

我试图从 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))

     }
    }

}

【问题讨论】:

    标签: swift avfoundation firebase-storage media-player avkit


    【解决方案1】:

    如果您想使用 HTTP URL 将数据从 Firebase 存储传递到播放器,您需要generate a download URL。下载 URL 是一个 URL,它为任何人(拥有该 URL)提供对数据的只读访问权限,因此您可以在播放器上设置它。

    如果您不想使用下载 URL(因为这可能会与其他无需登录即可访问数据的用户共享),您必须找到可以直接向其传递数据的播放器,而不是通过 URL。

    【讨论】:

    • 谢谢,我花了很多时间做可能永远不会工作的事情。现在我将尝试找到一些方法来制作自定义播放器,因为我想在应用程序下方放置一些 cmets 和 likes。无论哪种方式,投票给你!
    【解决方案2】:

    这是我在 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()
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-12-31
      • 1970-01-01
      • 2021-11-10
      • 1970-01-01
      • 2020-09-03
      • 2012-03-18
      • 2019-08-13
      • 1970-01-01
      • 2020-05-05
      相关资源
      最近更新 更多