You are browsing the archive for recording.

Tip #4: Storing Media Files Properly in Cordova iOS apps

December 15, 2014 in Apache Cordova, iOS

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:

  1. Create a Media object (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).
  2. Start recording the audio by calling recordingMedia.startRecord().
  3. After completing the audio recording, call recordingMedia.stopRecord() and then release the used Media object by calling recordingMedia.release()

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.

Reference:

“JavaScript Mobile Application Development” Book:

Skip to toolbar