My codes are to:
- Select a local Mp3 Uri
- Upload it to Firebase
- then retrieve that mp3 metadata such as name, artist, album cover,...
- Upload the album cover to Firebase
- Create a new object in the Firebase Realtime Database including the mp3 and cover download links, the name, and artist.
I have a Create the String
Then inside the mp3 upload onSucess() I retrieve the metadatas and upload the mp3 album cover: Uploading the album cover and assign the downloadLink to the str created above.
Finally create a song object and push it to he Realtime db: push song object to db
but the str is = "". And the result on the db is like this db result
I have checked inside the onSucess() in the 2nd img, the str is = the downloadLink but when moved to the 3rd img it = "" again.
I have tried static, non-static, local variable, etc for the str but no use.
Here is the full code
private static String str = "";
private void uploadFile() {
if (!filename.getText().toString().equals("") && imgView != null) {
StorageReference mp3ref = mp3StorageRef.child(SongName.getText().toString().trim() + "_" + System.currentTimeMillis() + "." + getFileExtension(Mp3_uri));
mp3ref.putFile(Mp3_uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
progressBar.setProgress(0);
}
}, 500);
MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
mediaMetadataRetriever.setDataSource(getContext(), Mp3_uri);
String name = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE);
String artist = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST);
byte[] art = mediaMetadataRetriever.getEmbeddedPicture();
StorageReference cover = coverStorageRef.child("cover_" + name + "_" + System.currentTimeMillis() + ".jpg");
cover.putBytes(art).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Task<Uri> urlTask = taskSnapshot.getStorage().getDownloadUrl();
while (!urlTask.isSuccessful()) ;
Uri downloadUrl = urlTask.getResult();
str = (downloadUrl.toString());
}
});
Task<Uri> urlTask = taskSnapshot.getStorage().getDownloadUrl();
while (!urlTask.isSuccessful()) ;
Uri downloadUrl = urlTask.getResult();
Song song = new Song(name, artist, str, downloadUrl.toString());
String uploadId = databaseReference.push().getKey();
databaseReference.child(uploadId).setValue(song);
Toast.makeText(getContext(), "Upload Success", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(getContext(), "Upload Failed", Toast.LENGTH_SHORT).show();
}
}).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(@NonNull UploadTask.TaskSnapshot snapshot) {
double Progress = (100.0 * snapshot.getBytesTransferred() / snapshot.getTotalByteCount());
progressBar.setProgress((int) Progress);
}
});
} else {
Toast.makeText(getContext(), "No File Selected", Toast.LENGTH_SHORT).show();
}
}
A way to upload 2 files (mp3 and jpg) and retrieve both files download Links.