0
votes

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();
}
2

2 Answers

0
votes

There may be situations where you don't need the actual data for a stored file, but rather will want the URL. You can do this in a similar fashion as the last two examples by using the getDownloadUrl() method on your StorageReference, which will give you a Uri pointing to the file's location.

storageRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
    @Override
    public void onSuccess(Uri uri) {
        Log.e("Tuts+", "uri: " + uri.toString());
        //Handle whatever you're going to do with the URL here
    }
});

You can refer here to play audio from the URL : Link

0
votes

finally i did it ! the problem was in firebase storage rules that chaneged to

service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
  allow read, write ;
}

} }