使用phonegap,Android上传媒体文件

时间:2014-01-08 17:51:07

标签: android audio file-upload cordova parse-platform

我尝试使用phonegap的File API上传捕获的音频文件。我将音频文件记录为mp3文件,然后在文件系统中找到该文件,然后将该文件上传到服务器。文件的录制工作正常,我可以在我的本地存储中播放。上传文件似乎也运行正常,但我上传时无法播放mp3文件。所以我要问的是什么;是我尝试上传正确的录制文件的路径,我实际从媒体文件中获取正确的base64编码字符串,还是我创建了一个新文件,因为我的设备无法找到我正在寻找的文件。目前我正在使用Android,但如果它是iOS的任何怪癖我也很想知道它们。当我创建mp3文件时,它存储在我的/ storage / emulated / 0目录中。当我在Firefox中打开mp3文件时,mp3文件的持续时间只有1秒,应该是7秒。可能是什么问题呢?我非常感谢我能得到的每一个帮助,你可以在下面看到我的代码。

 // 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文件,但我无法播放它。我非常感谢我能得到的每一个帮助

1 个答案:

答案 0 :(得分:0)

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

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