I wanna use firebase for the first time and i just upload Audio on storage and noting else to firebase database .. i want a exact code that i can load this Audio to my app how can i do it please give me a step by step solution and this is my last code that have an error and doen't play anything thank for your help error is:
"error": { "code": 403, "message": "Permission denied. Could not perform this operation" }} java.io.IOException: { "error": { "code": 403, "message": "Permission denied. Could not perform this operation" }} at bjr.a(:com.google.android.gms.DynamiteModulesC@11975436:148) at bjr.a(:com.google.android.gms.DynamiteModulesC@11975436:80) at bjl.onTransact(:com.google.android.gms.DynamiteModulesC@11975436:8) at android.os.Binder.transact(Binder.java:387) at com.google.android.gms.internal.zzed.zzb(Unknown Source) at com.google.android.gms.internal.acb.zzhM(Unknown Source) at com.google.android.gms.internal.acf.zze(Unknown Source) at com.google.android.gms.internal.abu.zza(Unknown Source) at com.google.firebase.storage.zzb.run(Unknown Source) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) at java.lang.Thread.run(Thread.java:818)
and cod is :
{
public class MainActivity extends AppCompatActivity implements MediaPlayer.OnPreparedListener {
public String storageLocation ="gs://ooks-98f14.appspot.com/SampleAudio.mp3";
private MediaPlayer mMediaplayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fetchAudioUrlFromFirebase();
}
public void fetchAudioUrlFromFirebase() {
mMediaplayer = new MediaPlayer();
mMediaplayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
final FirebaseStorage storage = FirebaseStorage.getInstance();
// Create a storage reference from our app
StorageReference storageRef = storage.getReferenceFromUrl(storageLocation);
// StorageReference spaceref = storageRef.child("audio/SampleAudio_0.4mb (1).mp3");
storageRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
try {
final String url = uri.toString();
mMediaplayer.setDataSource(url);
mMediaplayer.setOnPreparedListener(MainActivity.this);
mMediaplayer.prepareAsync();
mMediaplayer.start();
// Download url of file
// final String url = uri.toString();
// mMediaplayer.setDataSource(url);
// wait for media player to get prepare
// mMediaplayer.setOnPreparedListener(MainActivity.this);
// mMediaplayer.prepareAsync();
} catch (IOException e) {
e.printStackTrace();
}
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.i("TAG", e.getMessage());
}
});
}
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}