Harry.Ajay
Harry.Ajay

Reputation: 1

How to download Video URLString from Firebase Database not Storage in Swift

I have been looking everywhere for how to get a videos URLString out of Firebase tree structure but can’t find it anywhere. Everything seems to be showing only how to play directly from Firebase Storage. I have already created a URLString in a tree node in Firebase Database which points to a video in Storage. I’m trying to get the urlString of highlightVideo from that tree.

Here’s where I’m up to with the code:

 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()
}}

Upvotes: 0

Views: 2536

Answers (2)

Manisha Joshi
Manisha Joshi

Reputation: 51

You need to fetch video url from StorageRef. Otherwise the url may not be in correct format.! While setting the url to StorageRef save it in your UserData and while Fetching Data below you get your snapshot.value you can try as:

To videoUrl pass you snapshot key ie dictonary["highlightedVideo"]

    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
        }

            }
        })
    }

Upvotes: 1

Mike McDonald
Mike McDonald

Reputation: 15953

I have a feeling that the issue here is that the video is not in the correct format (needs to be H.264 or MPEG-4 in .mp4, .m4v, .mov [or an HLS video for live streaming]), and the content type should be set appropriately (video/mp4, video/x-m4v, video/quicktime). Can you confirm these?

Upvotes: 1

Related Questions