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

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???

Are you releasing the media when you stop recording?

@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!

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.

2 Likes

@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:

1 Like

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