Tip #4: Storing Media Files Properly in Cordova iOS apps
In order to avoid being surprised by the loss of your recorded audio file in iOS, you should be aware of how to properly record and store a media file in Cordova iOS.
Generally, in order to record an audio file in Apache Cordova, you can use the Cordova
Media object as follows:
var recordingMedia = new Media(mediaFilePath, recordingSuccess, recordingError); // Start Recording Audio recordingMedia.startRecord(); // Stop Recording Audio recordingMedia.stopRecord(); // Release Media resource recordingMedia.release();
As shown in the code above, in order to record an audio file in Apache Cordova, you need to do the following:
- Create a
recordingMedia) and specify
mediaFilePath(the path of the audio file),
recordingSuccess(the success callback which will be called if the media operation succeeds),
recordingError(the error callback which will be called if the media operation fails).
- Start recording the audio by calling
- After completing the audio recording, call
recordingMedia.stopRecord()and then release the used
Mediaobject by calling
In iOS, if you set
mediaFilePath to the audio file name only without specifying any path (e.g
"test.wav"), you may be surprised to find your audio file stored under the iOS app’s
tmp directory (which is located under the iOS app’s sandbox directory). It is very important to be aware that the iOS app’s
tmp directory content can be deleted automatically by iOS at anytime by iOS.
In order to avoid losing your app’s recorded files, just place the
"documents://" prefix before the audio file name as follows.
var recordingMedia = new Media("documents://test.wav", recordingSuccess, recordingError);
Doing this will make the recorded audio file stored under the iOS app’s
Documents directory. The iOS app’s
Documents directory is located under the app’s sandbox directory and is suitable for storing the app files.