Upload recorded audio file using media plugin to firebase

hello

i’m working on a app that use cordova-plugin-media to record and audio file , and now i want to upload this audio file to firebase storage , for now i can locat the file but when i try to upload it i get an error :slight_smile:

"Error: Failed to execute ‘atob’ on ‘Window’: The string to be decoded is not correctly encoded.

here is my code

Utils.js

 audioDataURItoBlob: function(dataURI) {
  var byteString = atob(dataURI.split(',')[1]);
  var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
  var ab = new ArrayBuffer(byteString.length);
  var ia = new Uint8Array(ab);
  for (var i = 0; i < byteString.length; i++) {
      ia[i] = byteString.charCodeAt(i);
  }
  console.log('Inside data dataURI');
  var blob =  new Blob([ia], {type: mimeString});
  return blob;
}

getAudio: function(audioSource) {

    var audioURI = "data:audio/mpeg;base64," + audioSource;
    var file = Utils.dataURItoBlob(audioURI);
    
    //Create and set Meta Type to Firebase Storage Ref.
    var storageRef = firebase.storage().ref();
    var metadata = {
      'contentType': 'audio/mpeg'
    };
    Utils.show();


    //Refer to images folder of Firebase Storage.
    storageRef.child('audios/' + Utils.generateFilename()).put(file, metadata).then(function(snapshot) {
      //File successfully uploaded to Firebase Storage.
      var url = snapshot.metadata.downloadURLs[0];
      // $scope.sendMessage('image', url);
      
      $rootScope.$broadcast('audioUploaded', {
        audioUrl: url
      });
    }).catch(function(error) {
      console.log('error' + error);
      //Show Error.
      Utils.message(Popup.errorIcon, Popup.uploadImageError);
    });
  

}

Controllers.js

var extension = '.mp3';
var audio = null;

$scope.startRecording = function() {    
    audio = new Media(Utils.generateFilename() + extension, function(e){console.log(e, "success");}, function(e)  {console.log(e, "error");});
    audio.startRecord();

};
$scope.sendAudioMsg = function() {
    audio.stopRecord();
    audio.play();// testing if the file exist
    if(device.platform == "iOS")
    {
         var path = cordova.file.tempDirectory;
     }
     else if(device.platform == "Android")
     {
        var path = cordova.file.externalRootDirectory;
     }
     var filepath = path + "filename" + extension
     var filename = "filename" + extension;

     Utils.getAudio(filepath);
 };

can any one help me with this??

thanks

2 Likes

Were u able to sort this out? Struggling with a similar problem.

no one replies regarding this issue…been trying to get help in the forum :frowning: