【发布时间】:2014-01-10 03:11:14
【问题描述】:
我正在尝试使用 phonegap 的文件 API 上传捕获的音频文件。我将音频文件录制为 mp3 文件,然后在文件系统中找到该文件,然后将文件上传到服务器。文件的录制工作正常,我可以在本地存储中播放它。文件的上传似乎也可以正常工作,但上传时我无法播放 mp3 文件。所以我在问什么;是我尝试上传正确的录制文件的路径,我是否真的从媒体文件中获取了正确的 base64 编码字符串,或者我是否正在创建一个新文件,因为我的设备找不到我正在寻找的文件。目前我正在使用 Android,但如果它是 iOS 的任何怪癖,我真的很想知道它们。当我创建 mp3 文件时,它存储在我的 /storage/emulated/0 目录中。当我在 Firefox 中打开 mp3 文件时,mp3 文件的持续时间只有 1s,而应该是 7s。可能是什么问题呢?我非常感谢我能得到的每一个帮助,你可以在下面看到我的代码。
// The src of the mp3 file, works when I play it after in my local storage
src = "recording.mp3";
mediaRec = new Media(src, onSuccess, onError);
// Record audio
mediaRec.startRecord();
// Stop recording after 7 sec
mediaRec.stopRecord();
// get the file from the file system (local storage)
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
// Get the file from the file system
function gotFS(fileSystem) {
fileSystem.root.getFile("recording.mp3", null, gotFileEntry, fail);
}
function gotFileEntry(fileEntry) {
fileEntry.file(gotFile, fail);
}
// the file is successfully retreived
function gotFile(file){
readDataUrl(file);
}
// turn the file into a base64 encoded string, and update the var base to this value.
function readDataUrl(file) {
var reader = new FileReader();
reader.onloadend = function(evt) {
alert(evt.target.result);
base = evt.target.result;
};
reader.readAsDataURL(file);
}
// later when this is done I upload the file to something called parse. This seems to work fine,
var file = new Parse.File("sound.mp3", { base64: base }); file.save().then(function() {
Object.set("soundy", file);
}, function(error) {
alert("an error");
// The file either could not be read, or could not be saved to Parse.
});
该文件现在已成功保存为解析对象中的 mp3 文件,但我无法播放它。我非常感谢我能得到的每一个帮助
【问题讨论】:
-
好问题!!!!!!!我也有同样的问题!!!!!!如果它没有先到这里,我会在找到它时发布答案!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
标签: android audio file-upload cordova parse-platform