简体   繁体   English

从firebase存储下载并播放.mp3文件到内存

[英]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. 我需要从我的firebase存储gs://fir-b9532.appspot.com/songs/song1.mp3下载一个mp3文件并将其播放到我的Android应用程序中。 I want to store the mp3 file temporary in my device's memory, play it with a mediaplayer and then bring a new song. 我想将mp3文件临时存储在我设备的内存中,用媒体播放器播放然后再播放一首新歌。

I follow the firebase manual writing the following simple code but seems not working. 我按照firebase手册编写以下简单代码,但似乎无法正常工作。 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
        }
    });

}


 }

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 . R.id.bytes是一个名为bytes的id资源。 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. 还要记住,MediaPlayer无法播放存在于字节数组中的媒体。 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) . 您必须将字节数组保存到文件中,并将MediaPlayer指向该文件以便使用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 "). 而不是下载音频文件并暂时将其保存在内存中我通过实时流式传输来自firebase存储提供的完整URL的歌曲找到了另一种解决方案(“ 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: 我只是像这样加载我的MediaPlayer:

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 在本地下载它跟随Doug的响应和MediaPlayer中的Play Byte数组 - Android

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM