Reputation: 163
I am using firebase storage to upload audio from my android app and then download and play in my app. The audio file gets uploaded but when I play it from firebase storage,it comes in a video format like image below and does not play the audio. I am also setting metadata as audio/mp3 for it.
I tried to search about the same in firebase documentation and google but could not get my issue resolved.
Uri file = Uri.fromFile(new File(fileName));
//filename is audio stored in my phone's local storage.
StorageMetadata metadata = new StorageMetadata.Builder()
.setContentType("audio/mp3")
.build();
// Upload the file and metadata
UploadTask uploadTask = storageReference.child("audio/audio.mp3").putFile(file, metadata);
uploadTask.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
}
}).addOnPausedListener(new OnPausedListener<UploadTask.TaskSnapshot>() {
@Override
public void onPaused(UploadTask.TaskSnapshot taskSnapshot) {
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle unsuccessful uploads
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// Handle successful uploads on complete
// ...
Task<Uri> uri = taskSnapshot.getStorage().getDownloadUrl();
while(!uri.isComplete());
Uri url = uri.getResult();
Toast.makeText(MainActivity.this, "Upload Success, download URL " +
url.toString(), Toast.LENGTH_LONG).show();
}
});
Task<Uri> urlTask = uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
@Override
public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
if (!task.isSuccessful()) {
throw task.getException();
}
// Continue with the task to get the download URL
return storageReference.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
} else {
// Handle failures
// ...
}
}
});
I want the audio to be saved properly in firebase storage and then stream it in my app. Any help will be appreciated. Thank you!
Upvotes: 4
Views: 2061
Reputation: 300
Your problem could have been caused by MediaRecorder. I have figured out that the output format should be set to AAC. Then the browser will be able to play it. I experienced this problem when I was trying to use React JS to play audio captured by MediaRecorder in Android, and I tried various formats (.3gp, .mp4, .wav), but this is the only one that worked. I suspect there are other ones, too.
So:
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.AAC_ADTS);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
Upvotes: 3
Reputation: 581
Try like this,
metadata = new StorageMetadata.Builder()
.setContentType("audio/mpeg")
.build();
uploadTask = storageRef.child("audio/"+file.getLastPathSegment()).putFile(file,metadata);
Upvotes: 0