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 :
@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.
@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));
});