Marius Djerv
Marius Djerv

Reputation: 265

Cordova New Media Record save to directory

Working on a recording app with Cordova and using new Media for recording. I'm also using cordoba-file-plugin for creating directories etc. So when the app initialising I'm running

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fs){
   app.root = fs;
}, function(error){
   console.log(error);
});

And then I'm running this code:

app.root.root.getDirectory('appname', {create:true}, function(dirEntry){
   dirEntry.getDirectory('recordings', {create:true}, function(subDirEntry) {
      app.recordings = subDirEntry;
   }, function(error){
      console.log(error);
   });
}, function(error){
   console.log(error);
});

Then I have the recoding script and I would like to save the file directly to app.recordings, the directory I created for this app. But nothing shows up.

var path = app.recordings.nativeURL;
var filename = path + "recording-" + new Date().getTime() + ".mp3";
var mediaRec = new Media(filename, app.services.audio.success, app.services.audio.error);
mediaRec.startRecord();
mediaRec.stopRecord();

I keep getting a tmprecording-12351834581925.3gp in root.

Upvotes: 0

Views: 977

Answers (1)

Marius Djerv
Marius Djerv

Reputation: 265

On device ready

if(device.platform == "iOS"){
   app.path = cordova.file.tempDirectory;
   app.extension = ".wav";
   app.mimeType = "audio/wav";
} else if(device.platform == "Android"){
   app.path = cordova.file.externalRootDirectory;
   app.extension = ".amr";
   app.mimeType = "audio/amr";
}

window.resolveLocalFileSystemURL(app.path, function(fileSystem){
   fileSystem.getDirectory("AppName", {create:true},function(dirEntry){
      dirEntry.getDirectory("Recording", {create:true} , function(subEntry){
            app.recording = subEntry.nativeURL;
         }, function(error){
            console.log(error);
         });
   }, function(error){
      console.log(error);
   });
},function(error){
   console.log(error);
});

Then on recording

var filename = app.recording + new Date().getTime() + app.extension;
var mediaRec = new Media(filename, success, error);

The files are now saved in a preferred location where I now have control and access to the files.

Reading files in Recordings

window.resolveLocalFileSystemURL(app.recording ,function (fileSystem) {
   var reader = fileSystem.createReader();
   reader.readEntries(function (entries) {
      console.log(entries);
      //loop all entries
   }, function(error){
      console.log(error);
   });
}

Upvotes: 3

Related Questions