Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download and play .mp3 file from firebase storage to memory

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 temporary in my device's memory, play it with a mediaplayer and then bring a new song.

I follow the firebase manual writing the following simple code but seems not working. ANy ideas whats going wrong?

package com.example.andreaskonstantakos.firebase;

 import android.media.MediaPlayer;
 import android.support.annotation.NonNull;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import com.google.android.gms.tasks.OnFailureListener;
 import com.google.android.gms.tasks.OnSuccessListener;
 import com.google.firebase.storage.FirebaseStorage;
 import com.google.firebase.storage.StorageReference;


 public class MainActivity extends AppCompatActivity {

     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);

         FirebaseStorage storage = FirebaseStorage.getInstance();

         StorageReference storageRef = storage.getReference();
         StorageReference songsRef = storageRef.child("songs");
         StorageReference song1Ref = storageRef.child("songs/song1.mp3");

         final long ONE_MEGABYTE = 1024 * 1024;
         song1Ref.getBytes(ONE_MEGABYTE).addOnSuccessListener(new OnSuccessListener<byte[]>() {
             @Override
             public void onSuccess(byte[] bytes) {
                 // Data for "songs/song1.mp3" is returns, use this as needed
                 MediaPlayer md = MediaPlayer.create(MainActivity.this,R.id.bytes);
                 md.setLooping(false);
                 md.start();
             }
    }).addOnFailureListener(new OnFailureListener() {
             @Override
             public void onFailure(@NonNull Exception exception) {
                 // Handle any errors
        }
    });

}


 }
like image 614
Andreas Konstantakos Avatar asked Jan 05 '23 02:01

Andreas Konstantakos


2 Answers

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
    }
like image 146
Andreas Konstantakos Avatar answered Jan 09 '23 20:01

Andreas Konstantakos


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.

like image 26
Doug Stevenson Avatar answered Jan 09 '23 21:01

Doug Stevenson