如何从Firebase数据库下载视频URLString而不是在Swift中存储

时间:2016-11-17 16:10:12

标签: swift firebase firebase-realtime-database firebase-storage

我一直在寻找如何从Firebase树结构中获取视频URLString,但无法在任何地方找到它。一切似乎只显示如何直接从Firebase存储中播放。我已在Firebase数据库的树节点中创建了URLString,该节点指向存储中的视频。我正试图从该树中获取urlString highlightVideo

以下是我在使用代码的地方:

 private func setupPlayerView() {

    FIRDatabase.database().reference().child("users").child(FIRAuth.auth()!.currentUser!.uid).observeEventType(.ChildAdded, withBlock: { (snapshot) in
        guard let dictionary = snapshot.value as? [String: String] else { return}

        let urlString = dictionary["highlightVideo"]
    }, withCancelBlock: nil)

    if let url = NSURL(string: urlString) { ---code breaks on urlstring
        player = AVPlayer(URL: url)

        let playerLayer = AVPlayerLayer(player: player)
        videoView.layer.addSublayer(playerLayer)
        playerLayer.frame = CGRect(x: 0, y: 0, width: frame.width, height: frame.width * (9/16))

        player?.play()
}}

2 个答案:

答案 0 :(得分:1)

我觉得这里的问题是视频格式不正确(需要if(get_page_template() == (get_stylesheet_directory() . '/custom-template.php')){ //your stuff } 中的H.264或MPEG-4,.mp4.m4v [或实时流式传输的HLS视频]),应适当设置内容类型(.movvideo/mp4video/x-m4v)。你能证实这些吗?

答案 1 :(得分:1)

您需要从StorageRef中获取视频网址。 否则网址格式可能不正确。将URL设置为StorageRef时将其保存在UserData中,并在下面获取数据时获取snapshot.value,您可以尝试:

to videoUrl传递快照键,即dictonary [" highlightVideo"]

    let storageRef = Storage.storage().reference(forURL: videoURL)
    storageRef.getData(maxSize: INT64_MAX) { (data, error) in
        if let error = error {
            print("Error downloading image data: \(error)")
            return
        }
        storageRef.getMetadata(completion: { (metadata, metadataErr) in

            if let error = metadataErr {
                print("Error downloading metadata: \(error)")
                return
            }
            if (metadata?.contentType == "image/gif") {
                print("It is Gif Image")
            } else {
                let downloadUrl = metadata?.downloadURL()
                if downloadUrl != nil{
     print(downloadUrl)
           //You will get your Video Url Here
        }

            }
        })
    }