Question

Im trying to upload a captured audio file by using phonegap's File API. I record the audio file as an mp3 file, and then locate the file in the filesystem, and later upload the file to a server. The recording of the file works fine, and I can play it in my local storage. The uploading of the file also seems to work fine, but Im not able to play the mp3 file when its uploaded. So what Im asking; is the path to the recorded file I try to upload correct, and do I actual get the correct base64 encoded string from the media file, or am I creating a new file since my device cannot find the file Im looking for. For the moment Im working with Android, but If it is any quirks for iOS I would really like to know them as well. When I create the mp3 file, it is stored in my /storage/emulated/0 directory. When I open the mp3 file in Firefox, the duration of the mp3 file is only 1s, when it should be 7s. What could be the problem? I really appreciate every help I can get, you can see my code below.

 // 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.
  });

The file is now successfully saved as an mp3 file in a parse object, but Im unable to play it. I really appreciate every help I can get

Was it helpful?

Solution

maybe your issue is that the file reading is assynchronous and the file is not compleately read when you try to upload it?

You could try to move your uload code inside of the callback function in readDataUrl() (just after the base = evt.target.result; line)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top