我需要從我的firebase存儲gs://fir-b9532.appspot.com/songs/song1.mp3下載mp3文件並將其播放到我的android應用程序中。 我想將mp3文件臨時存儲在設備的存儲器中,用mediaplayer播放,然後播放一首新歌。從firebase存儲下載並播放.mp3文件到內存
我按照firebase手冊編寫以下簡單的代碼,但似乎不工作。任何想法發生了什麼問題?
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
}
});
}
}
的excecution繼續onFailure處的方法,從不在的onSuccess –