gpt4 book ai didi

android - 用phonegap上传媒体文件,Android

转载 作者:行者123 更新时间:2023-12-02 23:53:22 25 4
gpt4 key购买 nike

我试图通过使用phonegap的File 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文件,位于解析对象中,但是我无法播放。我非常感谢我能获得的每一次帮助

最佳答案

也许您的问题是文件读取是异步的,并且在尝试上传文件时未完全读取文件?

您可以尝试将uload代码移动到readDataUrl()中的回调函数中(在base = evt.target.result;行之后)

关于android - 用phonegap上传媒体文件,Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21002937/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com