2016-08-02 180 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