← Plugins / Media
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.
cordova plugin add cordova-plugin-media
var media = new Media(src, onSuccess, onError, onStatusChange);
| Argument | Description |
|---|---|
src | A URI to the audio file to play, or the path to record into. |
onSuccess | Called when playback or recording finishes successfully. |
onError | Called with a MediaError on failure. |
onStatusChange | Called with a status constant whenever the state changes. |
| Method | Description |
|---|---|
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. |
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).
Play a bundled sound and release it when it ends:
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();
}
release() when you're finished — native audio handles are limited.