I need to download an mp3 file from my firebase storage gs://fir-b9532.appspot.com/songs/song1.mp3 and play it into my android application. I want to store the mp3 file temporar
This line isn't doing at all what you expect:
MediaPlayer md = MediaPlayer.create(MainActivity.this,R.id.bytes);
R.id.bytes
is an id resources called bytes
. It contains an integer. It's not the array of bytes that you're receiving in your success listener.
Bear in mind also that MediaPlayer can't play media that exists in an array of bytes. You'll have to instead save the array of bytes to a file, and point MediaPlayer to that file for playback with create(Context context, Uri uri). Then you should probably delete the file when you're done.
Instead of downloading the audio file and temporarly keep it on the memory i found another solution by live-streaming the song from the full URL that firebase storage provide ("https://firebasestorage.googleapis.com/v0/b/fir-b9532.appspot.com/o/songs%2Fsong1.mp3?alt=media&token=a4424b28-93c3-4a0c-a6b9-9136dcf63335").
I just load my MediaPlayer like this:
try {
MediaPlayer player = new MediaPlayer();
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setDataSource("https://firebasestorage.googleapis.com/v0/b/fir-b9532.appspot.com/o/songs%2Fsong1.mp3?alt=media&token=a4424b28-93c3-4a0c-a6b9-9136dcf63335");
player.prepare();
player.start();
} catch (Exception e) {
// TODO: handle exception
}
you can stream like this
ref.getDownloadUrl().addOnSuccessListener{
try {
mediaPlayer.setDataSource(uri);
} catch (IOException e) {}
}
to download it locally follow Doug's response and this Play Byte array in MediaPlayer - Android