What I want is to save mp3 files on Firebase Storage and then stream it to an Android device. All the tutorials on Firebase discuss about the image file upload and download.
If there are any other cloud that is more easy than Firebase to store and stream audio for android, then please suggest.
2020: Yes, firebase storage video streaming is easy and possible.
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/…
You have two choices if you want to get files out of a Firebase Storage bucket. Use the full download url that you can get from a Storage Reference that points to the file path in your bucket. Use a download task (Android or iOS) to fetch the data from the file.
Firebase provide StreamDownloadTask as Mike has suggested, for getting InputStream but unfortunately MediaPlayer doesn't accept a direct stream. Now we have 2 options if we strictly like to continue with InputStream, as far as I know -
a. Write a stream in the temporary file and pass it to the media player. Personally, I don't recommend this approach. It includes unnecessary data writing and you also have to maintain synchronization between streamed data, file writing and media play.
b. Create StreamProxy server as npr news App doing. You can find the source code of App here.
Simple way to play Music file from Firebase is to first fetch the download URL by using storage reference and pass that URL to media player as a dataSource. Below is the code snippet -
public class MainActivity extends AppCompatActivity implements MediaPlayer.OnPreparedListener { private MediaPlayer mMediaplayer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mMediaplayer = new MediaPlayer(); mMediaplayer.setAudioStreamType(AudioManager.STREAM_MUSIC); fetchAudioUrlFromFirebase(); } private void fetchAudioUrlFromFirebase() { final FirebaseStorage storage = FirebaseStorage.getInstance(); // Create a storage reference from our app StorageReference storageRef = storage.getReferenceFromUrl("PATH_OF_YOUR_AUDIO_FILE"); storageRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() { @Override public void onSuccess(Uri uri) { try { // Download url of file final String url = uri.toString(); mMediaplayer.setDataSource(url); // wait for media player to get prepare mMediaplayer.setOnPreparedListener(MainActivity.this); mMediaplayer.prepareAsync(); } catch (IOException e) { e.printStackTrace(); } } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { Log.i("TAG", e.getMessage()); } }); } @Override public void onPrepared(MediaPlayer mp) { mp.start(); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With