我正在使用firebase存储从我的Android应用程序上传音频,然后下载并在我的应用程序中播放。音频文件上传但是当我从firebase存储中播放时,它会出现如下图像的视频格式,并且不播放音频。我也将元数据设置为音频/ mp3。
我试图在firebase文档和谷歌中搜索相同但无法解决我的问题。
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
// ...
}
}
});
我希望音频在firebase存储中正确保存,然后在我的应用程序中流式传输。任何帮助将不胜感激。谢谢!
试试这样,
metadata = new StorageMetadata.Builder()
.setContentType("audio/mpeg")
.build();
uploadTask = storageRef.child("audio/"+file.getLastPathSegment()).putFile(file,metadata);