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:

  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:

This entry was posted in Apache Cordova, iOS and tagged , , , , , , by Hazem Saleh. Bookmark the permalink.

About Hazem Saleh

Hazem Saleh has more than eleven years of experience in Cloud, Mobile and Open Source technologies. He worked as a software engineer, technical leader, application architect, and technical consultant for many clients around the world. He is an Apache PMC (Project Management Committee) member and a person who spent many years of his life writing open source software. Beside being the author of the "JavaScript Unit Testing" book, "JavaScript Mobile Application Development" book, "Pro JSF and HTML5" book and the co-author of the "Definitive guide to Apache MyFaces" book, Hazem is also an author of many technical articles, a developerWorks contributing author and a technical speaker in both local and international conferences such as ApacheCon North America, Geecon, JavaLand, JSFDays, CON-FESS Vienna and JavaOne. Hazem is an XIBMer, he worked in IBM for ten years. Now, He is working for Nickelodeon New York as a Mobile Architect. He is also an OpenGroup Master Certified Specialist.

One thought on “Tip #4: Storing Media Files Properly in Cordova iOS apps

  1. Brilliant – thanks for this tip. I was tearing my hair out wondering why my WAV files were being stashed away in /tmp. Any other file paths we should know about? Especially for NoCloud storage backup etc.? How does the “document://” prefix affect Android devices? I haven’t got as far as testing my Cordova app on Android as yet…

Comments are closed.