Many people use the Media Plugin to record audio in cordova apps. Here is an sample code that shows how to record:
document.addEventListener("deviceready", function(){
if(device.platform == "iOS")
{
extension = ".wav";
}
else if(device.platform == "Android")
{
extension = ".amr";
}
}, false);
var audio = new Media("filename" + extension, function(e){console.log(e, "success");}, function(e){console.log(e, "error");});
//start recording
audio.startRecord();
//stop recording
audio.stopRecord();
In case you want to upload or manipulate the file after recording then here is the code to find the file location:
{
var path = cordova.file.tempDirectory;
}
else if(device.platform == "Android")
{
var path = cordova.file.externalRootDirectory;
}
alert("File path is: " + path + "filename" + extension);
In iOS the file is stored in temporary directory. Files in this directory can be deleted by the operation system any time therefore its recommended to store files in documents
directory using. Below code shows how to record and put file in documents
directory:
And then to find location of the file we can use cordova.file.documentsDirectory
instead of cordova.file.tempDirectory
.
Note: Make sure you have file plugin installed.