How can i use $cordovaFile to delete and upload file that create by $cordovaMedia.newMedia


#1

I am coding an voice communication apps, but a problem trouble me a lot .
I create a media file by $cordovaMedia.newMedia like:

var mediaSrc = "documents://test.wav";
var mediaSource = $cordovaMedia.newMedia(mediaSrc);
media = mediaSource.media;
$cordovaMedia.startRecord(media);
... //do stop record

But when i try to delete the file an error occur, the code to delete the file is :

var filepath = cordova.file.documentsDirectory + "test.wav";
$cordovaFile.removeFile(filepath)
            .then(function(result) {
                    alert("success");
                    // Success!
            }, function(err) {
                    alert(JSON.stringify(err));
                    // Error
});

the errorCode is 5 which means “FileError.ENCODING_ERR”

and I try to remove “file:\” form my filepath

  var filepath = cordova.file.documentsDirectory.replace(/file:\/\//,"") + "test.wav";
    $cordovaFile.removeFile(filepath)
                .then(function(result) {
                        alert("success");
                        // Success!
                }, function(err) {
                        alert(JSON.stringify(err));
                        // Error
    });

but this time the errorCode is 1 which means “FileError.NOT_FOUND_ERR”

how can I delete the media file by $cordovaFile???


How to get audio file after recording (cordova-plugin-media)
#2

Are you releasing the media when you stop recording?


#3

@fiffy Did you get this figured out? I’m also having problems creating files in $cordovaMedia and accessing them with $cordovaFile, would appreciate any insight you found working through this!


#4

Ok, I lost a lot of hair trying to tackle a very similar issue with Ionic and $cordovaMedia.

Basically, you are doing the right thing to start off with. On iOS especially, I had to prefix the file name with ‘documents://’ so that the file was placed in the correct data folder on the device.

For deleting though, you simply have to strip off the leading ‘documents://’ prefix and then $cordovaFile will delete the file. In your case - just specifying ‘test.wav’ ONLY should work. So, to extrapolate your example:

var mediaSrc = "documents://test.wav";
var mediaSource = $cordovaMedia.newMedia(mediaSrc);
media = mediaSource.media;
$cordovaMedia.startRecord(media);
... //do stop record

var filepath = mediaSrc.substring(12);
$cordovaFile.removeFile(filepath)
            .then(function(result) {
                    alert("success");
                    // Success!
            }, function(err) {
                    alert(JSON.stringify(err));
                    // Error
});

should work…

The .substring(12) function just strips the first 12 characters from the filename, which is the documents:// bit we don’t want.


#5

@CyberFerret, you also need to pass the base path for documents directory as first parameter in the removeFile function like this… cordova.file.documentsDirectory

//recording_name = 'documents://abc.wav'
  var name = recording_name.split('//')[1]; //abc.wav
  $cordovaFile.removeFile(cordova.file.documentsDirectory, name)
      .then(function (result) {
        console.log('Success: deleting audio file' + JSON.stringify(result));
      }, function (err) {
        console.log('Error: deleting audio file' + JSON.stringify(err));
      });

After few hours effort this works for me :slight_smile:


#6

Can I see entire code? How to record and play, on android?