我试图通过使用PhoneGap的文件API来上传捕获的音频文件。我将音频文件记录为MP3文件,然后在文件系统中找到文件,然后将文件上传到服务器。文件的录制工作正常,我可以在本地存储中播放。上传文件似乎还可以正常工作,但我无法在上传时播放MP3文件。那我问的是什么;是录制文件的路径我尝试上传正确,我实际获取媒体文件的正确Base64编码的字符串,或者我创建新文件,因为我的设备无法找到我正在寻找的文件。目前我正在使用Android,但如果它是iOS的任何怪癖,我也非常想知道它们。当我创建MP3文件时,它存储在我/存储/仿真/ 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.
  });
.

该文件现在已成功保存为Parse对象中的MP3文件,但我无法播放。我真的很感谢我可以获得

的每个帮助

有帮助吗?

解决方案

也许您的问题是文件读取是非同步的,并且在尝试上传它时,文件不会再读取?

您可以尝试在ReadDataurl()中的回调函数内部的Ulload代码(即将在base = evt.target.result;行之后)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top