DedrisFrameworkDedrisFramework

← Plugins  /  Media

🎵

Media

cordova-plugin-media
iOS Android 🌐 Browser

This official Apache Cordova plugin records and plays back audio files on the device. You work with a Media object that wraps a single sound or recording, with methods to play, pause, seek, capture and release it — handy for sound effects, voice notes and simple players.

Installation

terminal
cordova plugin add cordova-plugin-media

Creating a Media object

syntax
var media = new Media(src, onSuccess, onError, onStatusChange);
ArgumentDescription
srcA URI to the audio file to play, or the path to record into.
onSuccessCalled when playback or recording finishes successfully.
onErrorCalled with a MediaError on failure.
onStatusChangeCalled with a status constant whenever the state changes.

Methods

MethodDescription
play()Start or resume playback.
pause()Pause playback.
stop()Stop playback and reset to the start.
seekTo(ms)Jump to a position, in milliseconds.
getCurrentPosition(success, error)Return the current playback position in seconds.
getDuration()Return the total length in seconds (or -1 if unknown).
setVolume(level)Set the volume from 0.0 to 1.0.
startRecord() / stopRecord()Begin and end recording audio to src.
release()Free the underlying native audio resources.

Status constants

The onStatusChange callback receives one of: Media.MEDIA_NONE (0), Media.MEDIA_STARTING (1), Media.MEDIA_RUNNING (2), Media.MEDIA_PAUSED (3) or Media.MEDIA_STOPPED (4).

Usage example

Play a bundled sound and release it when it ends:

www/js/app.js
function playSound() {
  var media = new Media(
    "/android_asset/www/audio/chime.mp3",
    function () {
      console.log("Done");
      media.release();
    },
    function (err) {
      console.error("Media error: " + err.code);
    }
  );

  media.play();
}

Tips

📚 Source: the technical reference on this page (the Media object, methods and status constants) is based on the official Apache Cordova media documentation. Descriptions and examples have been rewritten in DedrisFramework's own words. Apache Cordova documentation is published by the Apache Software Foundation under the Apache License 2.0.
← Back to plugins Start a project