2016-08-02 195 views
0

我想上传音频并稍后下载。 我正在使用Firebase服务器进行此过程。从firebase上传/下载音频

有2个查询我坚持。任何解决方案将解决目的:

  1. 如何上传文件而不是blob文件。我相信应该有一种方法来上传文件,而不是首先将文件转换为blob然后上传(这是我现在正在执行的操作)。
  2. 我可以下载文件但无法播放。由于该文件首次转换为DataURL/ArrayBuffer,然后上传,我如何将其转换回音频(mp3/wav)?

的代码上传:

$scope.uploadFile = function(){ // uploading file 
var storageRef= firebase.storage().ref(); 
var filename= $scope.audio.src.substring($scope.audio.src.lastIndexOf("/")+1,$scope.audio.src.length); 
$scope.fname= filename; 
// fetching file to upload 
baseServ.dirEntry.getFile(filename, {}, function(entry){ 
    entry.file(function(gotfile){ 
    var reader= new FileReader(); 

    reader.onloadend= function(resultFile){ 
     console.log(resultFile); 
     var blob= new Blob([resultFile], {type:"audio/mp3"}); 
     // uploading to firebase server 
     var uploadTask = storageRef.child(filename).put(blob); 
     uploadTask.on('state_changed', function(snapshot){ 
     console.log("state_changed:" + filename + "::::Current State:" +snapshot.state); 
     }, function(error) { 
     alert("upload error"); 
     }, function() { 
     $scope.downloadURL = uploadTask.snapshot.downloadURL; 
     }); 
    } 
    //reading as dataUrl or ArrayBuffer 
    reader.readAsDataURL(gotfile); 
    }) 
}); 

}

并下载:

$scope.downloadFile= function(){ 
var ft= new FileTransfer(); 
ft.download($scope.downloadURL, baseServ.dirDownloads.nativeURL + $scope.fname, function(entry) { 
    // download is successful but unable to play when opening in my device 
    console.log("download complete: " + entry.toURL()); 
}, 
function(error) { 
    console.log("download error source " + error.source); 
}, 
false, 
{}); 

}

感谢。

回答

4

实时数据库是存储音频文件的错误工具。您应该使用Firebase存储。见Firebase Features

直接从Firebase客户端SDK存储和检索用户生成的内容,如图像,音频和视频 。

强大的上传和下载的背景下,无论网络 质量安全的客户端授权,由遍布火力地堡或谷歌云存储API的谷歌云 存储API访问支持 认证PB级别的数据存储集成的

+0

我只使用firebase。我只需要将MP3文件作为mp3上传,而不是作为blob。有没有办法? – Aashish

+1

Firebase包含Firebase存储。恭喜,如果您使用实时数据库存储音频,那么您使用的工具是错误的工具。 –

+0

正确地说,在与firebase进一步合作之后,我可以说实时数据库应该保持尽可能轻。作为管理员也会给你一段艰难的时光。 另一方面,存储可以与Heavy文件一起玩。 谢谢。 – Aashish

0

看来你不能上传音频本身。

在我看到的例子中,数据(用于图像文件)以dataURL/Base64字符串的形式上传,下载后重建文件。

由于我的音频文件很小,60-70 kb,我使用相同的,并重新与HTMLAudio元素重新构建文件。

0

为了给你一个简单的方法,只需在你的程序中使用这两种方法(从下面给出的链接)。

private void startRecording() { 
     mRecorder = new MediaRecorder();//mRecoreder has been declared globally 
     mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
     mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
     mRecorder.setOutputFile(mFileName); 
     mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 

     try { 
      mRecorder.prepare(); 
     } catch (IOException e) { 
      Log.e(LOG_TAG, "prepare() failed");//initiallize LOG_TAG as a string anything u like to get an indication 
     } 

     mRecorder.start(); 
    } 

    private void stopRecording() { 
     mRecorder.stop(); 
     mRecorder.release(); 
     mRecorder = null; 

     uploadAudio();//it is'nt in the link. it has been added by me to upload ur audio to firebase storage cloud 

    } 

现在上传的代码,添加调用方法uploadAudio()和它的身体是: -

私人无效uploadAudio(){

mProgressDialog.setMessage("Uploading Audio...");//declare it globally and initialize it with passing the current activity i.e this 
mProgressDialog.show(); 

StorageReference mFilePath = mStorageRef.child("Audio").child("new_audio.3gp"); 

Uri u = Uri.fromFile(new File(mFileName)); 
mFilePath.putFile(u).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() { 
    @Override 
    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { 

     mProgressDialog.dismiss(); 
     mTextView.setText("Audio has been Uploaded Successfully !!!"); 
    } 
}); 

}

这应该足够简单的音频上传。查看进一步定制的链接。 https://developer.android.com/guide/topics/media/mediarecorder.html