Download and play .mp3 file from firebase storage to memory

后端 未结 3 1536
借酒劲吻你
借酒劲吻你 2021-02-10 21:29

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

相关标签:
3条回答
  • 2021-02-10 21:58

    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.

    0 讨论(0)
  • 2021-02-10 22:01

    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
        }
    
    0 讨论(0)
  • 2021-02-10 22:07

    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

    0 讨论(0)
提交回复
热议问题