我正在尝试上传音频并稍后下载。 我正在使用firebase服务器进行此过程。
我遇到了2个问题。任何人的解决方案都可以解决目的:
要上传的代码:
$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,
{});
}
感谢。
答案 0 :(得分:4)
实时数据库是存储音频文件的错误工具。您应该使用Firebase存储。请参阅Firebase Features。
存储和检索用户生成的内容,如图像,音频和视频 直接来自Firebase客户端SDK。
在后台进行强大的上传和下载,无论网络如何 质量安全的客户端授权,与之集成 身份验证由Google Cloud支持的Petabyte规模数据存储 整个Firebase或Google云端存储API中的存储API访问
答案 1 :(得分:0)
您似乎无法上传音频本身。
在我看到的例子中,数据(用于图像文件)以dataURL / Base64字符串的形式上传,文件在下载后重建。
由于我的音频文件很小,为60-70 kb,我使用相同的文件并使用HTMLAudio元素重新构建文件。
答案 2 :(得分: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(),其主体是: -
private void 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