4

I am using ngCordova Capture to write this code by recording audio and send the base64 somewhere (via REST). I could get the Capture Audio to work but once it returns the audioURI, I cannot get the data from the filesystem as base64. My code is below:

$cordovaCapture.captureAudio(options).then(function(audioURI) {
  $scope.post.tracId = $scope.tracId;
  $scope.post.type = 'audio';
  console.log('audioURI:');
  console.log(audioURI);
  var path = audioURI[0].localURL;
  console.log('path:');
  console.log(path);

  window.resolveLocalFileSystemURL(path, function(fileObj) {
    var reader  = new FileReader();
    console.log('fileObj:');
    console.log(fileObj);

    reader.onloadend = function (event) {
      console.log('reader.result:');
      console.log(reader.result);
      console.log('event.result:');
      console.log(event.result);
    }
    reader.onload = function(event2) {
      console.log('event2.result:');
      console.log(event2.target.result);
    };
    reader.readAsDataURL(fileObj);
   console.log(fileObj.filesystem.root.nativeURL + ' ' + fileObj.name);
   $cordovaFile.readAsDataURL(fileObj.filesystem.root.nativeURL, fileObj.name)
   .then(function (success) {
         console.log('success:');
         console.log(success);
         }, function (error) {
         // error
         });
  });

Here is the output in console log:

enter image description here

So how do I get the base64 data from the .wav file?

I have been reading these links:

PhoneGap FileReader/readAsDataURL Not Triggering Callbacks

https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsDataURL

http://jsfiddle.net/eliseosoto/JHQnk/

http://community.phonegap.com/nitobi/topics/filereader_onload_not_working_with_phonegap_build_2_5_0

Community
  • 1
  • 1
HP.
  • 19,226
  • 53
  • 154
  • 253

1 Answers1

7

Had same problem, which I fixed using both the Cordova Capture and Cordova File plugin.

navigator.device.capture.captureAudio(function (audioFiles) {
    var audioFile = audioFiles[0],
        fileReader = new FileReader(),
        file;
    fileReader.onload = function (readerEvt) {
        var base64 = readerEvt.target.result;
    };
    //fileReader.reasAsDataURL(audioFile); //This will result in your problem.
    file = new window.File(audioFile.name, audioFile.localURL, 
                           audioFile.type, audioFile.lastModifiedDate, audioFile.size);
    fileReader.readAsDataURL(file); //This will result in the solution.
});
Niels Steenbeek
  • 4,692
  • 2
  • 41
  • 50
  • Is there a way to accomplish this with a video? – stackexchange12 Jan 13 '16 at 13:54
  • @stackexchange12 Yes, I did it for video as well. Use capture.captureVideo instead. – Niels Steenbeek Jan 13 '16 at 14:21
  • Could you have a look at my question @NielsSteenbeek? I could not get your solution to work unfortunately. Thanks so much. https://stackoverflow.com/questions/51254272/readasdataurl-does-not-process-file-from-captureaudio – Bart S Jul 09 '18 at 21:29