Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get base64 of audio data from Cordova Capture

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

like image 613
HP. Avatar asked Mar 17 '23 16:03

HP.


1 Answers

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.
});
like image 100
Niels Steenbeek Avatar answered Mar 25 '23 00:03

Niels Steenbeek