media.startRecord

Starts recording an audio file.

media.startRecord();

Description

Function media.startRecord is a synchronous function that starts recording an audio file.

Supported Platforms

  • Android
  • BlackBerry WebWorks (OS 5.0 and higher)
  • iOS
  • Windows Phone 7 and 8
  • Windows 8

Quick Example

// Record audio
// 
function recordAudio() {
    var src = "myrecording.mp3";
    var mediaRec = new Media(src,
        // success callback
        function() {
            console.log("recordAudio():Audio Success");
        },
        
        // error callback
        function(err) {
            console.log("recordAudio():Audio Error: "+ err.code);
        });

    // Record audio
    mediaRec.startRecord();
}

Full Example

<!DOCTYPE html>
<html>
  <head>
    <title>Device Properties Example</title>

    <script type="text/javascript" charset="utf-8" src="cordova-2.6.0.js"></script>
    <script type="text/javascript" charset="utf-8">

    // Wait for Cordova to load
    //
    document.addEventListener("deviceready", onDeviceReady, false);

    // Record audio
    // 
    function recordAudio() {
        var src = "myrecording.amr";
        var mediaRec = new Media(src, onSuccess, onError);

        // Record audio
        mediaRec.startRecord();

        // Stop recording after 10 sec
        var recTime = 0;
        var recInterval = setInterval(function() {
            recTime = recTime + 1;
            setAudioPosition(recTime + " sec");
            if (recTime >= 10) {
                clearInterval(recInterval);
                mediaRec.stopRecord();
            }
        }, 1000);
    }

    // Cordova is ready
    //
    function onDeviceReady() {
        recordAudio();
    }

    // onSuccess Callback
    //
    function onSuccess() {
        console.log("recordAudio():Audio Success");
    }

    // onError Callback 
    //
    function onError(error) {
        alert('code: '    + error.code    + '\n' + 
              'message: ' + error.message + '\n');
    }

    // Set audio position
    // 
    function setAudioPosition(position) {
        document.getElementById('audio_position').innerHTML = position;
    }

    </script>
  </head>
  <body>
    <p id="media">Recording audio...</p>
    <p id="audio_position"></p>
  </body>
</html>

Android Quirks

  • Android devices record audio in Adaptive Multi-Rate format. The specified file should end with a .amr extension.

BlackBerry WebWorks Quirks

  • BlackBerry devices record audio in Adaptive Multi-Rate format. The specified file must end with a .amr extension.

iOS Quirks

  • iOS only records to files of type .wav and returns an error if the file name extension is not correct.
  • If a full path is not provided the recording will be placed in the /documents/tmp directory. This can be accessed via the [File](../file/fileobj/fileobj.html) apis using [LocalFileSystem](../file/localfilesystem/localfilesystem.html).TEMPORARY. Subdirectories are not created at record time and must already exist. Thus, myRecording.wav will work but recordings/myRecording.wav will not if the recordings directory does not already exist at /documents/tmp/.
  • Files can be recorded and played back using the documents URI:

      var myMedia = new Media("documents://beer.mp3")
    

Tizen Quirks

  • This API is not supported on Tizen devices.