如何在firebase android上传视频/ mp3文件

时间:2016-10-20 05:45:22

标签: video firebase android-studio-2.0

我可以从firebase上传/下载图片,但不知道android.so中firebase上的上传视频或.mp3文件如何建议我。 提前谢谢。

1 个答案:

答案 0 :(得分:5)

根据docs中给出的答案重写Uploading MP3 file to Firebase Storage ends up being a video/mp3 file中的示例,似乎音频上传的完整工作示例如下所示:

// File or Blob
 file = Uri.fromFile(new File("path/to/audio.mp3"));

// Create the file metadata
metadata = new StorageMetadata.Builder()
        .setContentType("audio/mpeg")
        .build();

// Upload file and metadata to the path 'audio/audio.mp3'
uploadTask = storageRef.child("audio/"+file.getLastPathSegment()).putFile(file, metadata);

// Listen for state changes, errors, and completion of the upload.
uploadTask.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
    @Override
    public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
        double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
        System.out.println("Upload is " + progress + "% done");
    }
}).addOnPausedListener(new OnPausedListener<UploadTask.TaskSnapshot>() {
    @Override
    public void onPaused(UploadTask.TaskSnapshot taskSnapshot) {
        System.out.println("Upload is paused");
    }
}).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
        Uri downloadUrl = taskSnapshot.getMetadata().getDownloadUrl();
   }
});