This plugin provides the ability to record and play back audio files on a device.

NOTE: The current implementation does not adhere to a W3C specification for media capture, and is provided for convenience only. A future implementation will adhere to the latest W3C specification and may deprecate the current APIs.

Parameters

  • src: A URI containing the audio content. (DOMString)
  • mediaSuccess: (Optional) The callback that executes after a Media object has completed the current play, record, or stop action. (Function)
  • mediaError: (Optional) The callback that executes if an error occurs. (Function)
  • mediaStatus: (Optional) The callback that executes to indicate status changes. (Function)

Constants

The following constants are reported as the only parameter to the mediaStatus callback:

  • Media.MEDIA_NONE = 0;
  • Media.MEDIA_STARTING = 1;
  • Media.MEDIA_RUNNING = 2;
  • Media.MEDIA_PAUSED = 3;
  • Media.MEDIA_STOPPED = 4;

Methods

  • media.getCurrentPosition: Returns the current position within an audio file.
  • media.getDuration: Returns the duration of an audio file.
  • media.play: Start or resume playing an audio file.
  • media.pause: Pause playback of an audio file.
  • media.release: Releases the underlying operating system’s audio resources.
  • media.seekTo: Moves the position within the audio file.
  • media.setVolume: Set the volume for audio playback.
  • media.startRecord: Start recording an audio file.
  • media.stopRecord: Stop recording an audio file.
  • media.stop: Stop playing an audio file.

Additional ReadOnly Parameters

  • position: The position within the audio playback, in seconds.
    • Not automatically updated during play; call getCurrentPosition to update.
  • duration: The duration of the media, in seconds.

You can find examples for each method on source documentation page.
Quick Record and Play Example

var recordAudioSrc;
var mediaRec;
startRecordAudio = function() {
  if(device.platform==”Android”)
  recordAudioSrc = “myrecording”+new Date().getTime()+”.amr”;
else
  recordAudioSrc = “myrecording”+new Date().getTime()+”.wav”;
  mediaRec = new Media(recordAudioSrc,
    // success callback
    function() {
      document.getElementById(“audioRecord”).innerHTML = “Recording, speak to microphone.”;
    },
    // error callback
    function(error) {
      alert( error.code );
    }
  );

// Record audio
mediaRec.startRecord();
};

stopRecordAudio = function() {
mediaRec.stopRecord();
document.getElementById(“audioRecord”).innerHTML = “Audio record stopped.”;
document.getElementById(“playRecord”).removeAttribute(“disabled”);
};

playRecordedAudio = function() {
var media = new Media(recordAudioSrc,
// success callback
function() {
document.getElementById(“audioRecord”).innerHTML = “Listening…”;
},

// error callback
function(error) {
alert( error.code );
}
);

media.play();
document.getElementById(“playRecord”).setAttribute(“disabled”,”disabled”);
};

Note: Just use 3 buttons for this example. Start recording, stop recording and play recording.

Source Documentation:
http://plugins.cordova.io/#/package/org.apache.cordova.media

About The Author

Leave a Reply

Your email address will not be published. Required fields are marked *