Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to stream a mp3 audio from firebase storage

I'm developing an app that will stream mp3 file stored in firebase storage. Nearly I'm having 100 songs like song1,song2,song3..... if a song is selected I have to stream that particular song without downloading. In such case I need to write plenty of code because for each song I have to mention the firebase storage url. the url would be like

https://firebasestorage.googleapis.com/......song1.mp3?alt=media&token=8a8a0593-e8bb-40c7-87e0-814d9c8342f3

For each song the alt=media&token= part of the url varies, so I have to mention the unique url for all songs. But here I need some simplified coding to play the songs by mentioning its name alone from firebase storage.

Please suggest a way to stream the audio file by using its name alone that is stored in firebase storage.

like image 548
uma Avatar asked Dec 24 '16 17:12

uma


People also ask

How do I stream MP3 files?

To stream an mp3 file, upload your mp3 file to your public-html directory (or to a subdirectory of your public-html directory) preferably in MP3 format. OGG is also supported in Chrome and Firefox, but not in Safari and Internet Explorer. For example: Create a subdirectory (e.g., media) in your public-html directory.

Can Firebase store MP3?

You can explicitly specify metadata when you upload the file to Firebase. If you specify setContentType("audio/mpeg") it should map the file to an MP3 correctly.

Can I store songs in Firebase?

here for start you can upload any file in GC storage. firebase.google.com/docs/storage/android/upload-files to play/stream music you can use developer.android.com/guide/topics/media/… We've got a streaming download API: firebase.google.com/docs/reference/android/com/google/firebase/…

How do I get data from Firebase storage?

There are two ways to download files with Firebase Storage, using references in the SDK or a download URL. iOS and Android users can download files into memory, disk, or from a download URL. The web SDK can download files just from a download URL. StorageReference storageRef = FirebaseStorage.


2 Answers

You have two choices if you want to get files out of a Firebase Storage bucket.

  1. Use the full download url that you can get from a Storage Reference that points to the file path in your bucket.
  2. Use a download task (Android or iOS) to fetch the data from the file.

You can't get the file data any other way from within a mobile app. Firebase Storage doesn't support special media streaming protocols, such as RTSP.

like image 171
Doug Stevenson Avatar answered Sep 17 '22 17:09

Doug Stevenson


I did it using downloadUrl.

val storage = FirebaseStorage.getInstance()
storage.reference.child("songs/song1.mp3").downloadUrl.addOnSuccessListener({
    val mediaPlayer = MediaPlayer()
    mediaPlayer.setDataSource(it.toString())
    mediaPlayer.setOnPreparedListener { player ->
        player.start()
    }
    mediaPlayer.prepareAsync()
})
like image 27
vovahost Avatar answered Sep 18 '22 17:09

vovahost