shant_01
shant_01

Reputation: 172

How to play video using firebase storage

I'm developing an app that plays videos for different exercises and I am storing these videos using firebase storage. If anyone can help me figure out how to play the videos from the storage database that would be very helpful.

The video name will be equal to the property workout!.workoutTitle which is a struct I made somewhere else in the app.

Here's what I've managed to put together with some stuff online but I could use some help. Id like to pass the workout!.workoutTitle property to the getVideo function and play the video every time that function is called. Any help is appreciated!

var videoReference : StorageReference {
    return Storage.storage().reference().child("videos")
}

func getVideo(videoName: String) {
    let fileName = videoName + ".mp4"
    let downloadRef = videoReference.child(fileName)
    
    let downloadTask = downloadRef.getData(maxSize: (1024 * 1024 * 100)) { (data, error) in
        if let data = data {
            let videoPath = URL()
            videoController.playVideo(path: videoPath) //videoController is just a variable I use to manage playing videos
        }
    }
}

Upvotes: 0

Views: 755

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598740

Since you're calling downloadRef.getData(maxSize: (1024 * 1024 * 100) you're downloading the actual bytes from storage into your data variable.

If you want to get a URL and use that, use the approach shown in the documentation on generating a download URL:

let downloadTask = downloadRef.downloadURL { url, error in
  if let error = error {
    // Handle any errors
  } else {
    videoController.playVideo(path: url) 
  }
}

Upvotes: 1

Related Questions