What Are Media Files?
The Media API lets you record and play back audio on a device. You can typically play audio files loaded locally on the device, or play audio files retrieved from the Internet.
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.
var media = new media(src,mediaSuccess,[mediaError],[mediaStatus]);
The Media Object
The PhoneGap Media Object has following four parameters, three of which are optional:
src: A URL containing the audio content. (DOMString)
mediaSuccess: (Optional) The callback that is invoked after a Media object has completed the current play/record or stop action. (Function)
mediaError: (Optional) The callback that is invoked if there was an error. (Function)
mediaStatus: (Optional) The callback that is invoked to indicate status changes. (Function)
Using Media Methods
In this section, you learn how to use the main methods of the Media API. The methods allow you to play, record, and pause, among other things.
Note: If you're working with Version 0.9.4 or earlier on PhoneGap, you must ensure that all your callback functions are global in scope.
media.getCurrentPosition
To get the current position within an audio file, use media.getCurrentPosition, as shown here:
media.getCurrentPosition(mediaSuccess,[mediaError]);
The mediaSuccess function is a callback that is called with the current position, and the optional mediaError is a callback that's called in case of an error. The getCurrentPosition() method is an asynchronous function that returns the current position of the underlying audio file of a Media object.
Code:
var audio_file = new Media(src, onSuccess, onError);
var mediaTimer = setInterval(function ()
{
audio_file.getCurrentPosition(function (position)
{
if (position > -1)
{
console.log((position/1000) + " sec");
}
},
function (e)
{
console.log("Error getting pos=" + e);
});
}, 2000);
Supported Platform
- Android
- iOS
- Windows Phone 7 ( Mango )
getDuration
It actually returns the duration of an file.
media.getDuratiion()
getDuration() is a synchronous function that returns the duration (in milliseconds) of an audio file. To convert to seconds, you will need to divide by 1,000. If the duration is unknown, a value of -1 is returned. The following is sample code:
Code:
var audio_file = new Media(src, onSuccess, onError);
var count = 0;
var mytimerDur = setInterval(function () {
count = count + 200;
if (count > 2000)
{
clearInterval(mytimerDur);
}
var dur = audio_file.getDuration();
if (dur > 0)
{
clearInterval(mytimerDur);
document.getElementById('audio_duration').innerHTML = (dur/1000) + " sec";
}
}, 200);
Supported Platform
- Android
- iOS
- Windows Phone 7 ( Mango )
media.play
To start or resume playing an audio file, use the media.play() function, as shown in the following example:
media.play();
To actually play a file, you must pass in a URL, or a path to a locally stored file, as shown here:
Code:
function playAudio(url)
{
var audio_file = new Media(url, onSuccess, onError);
function()
{
console.log("playAudio():Audio Success");
}
function(err)
{
console.log("playAudio():Audio Error: "+err);
}
audio_file.play();
}
Supported Platform
- Android
- iOS
- Windows Phone 7 ( Mango )
media.pause
To pause the playing of an audio file, use the pause() method, as shown here:
media.pause();
Function media.pause() is a synchronous function that pauses playing an audio file. The following is sample code:
Code:
function playAudio(url)
{
var audio_file= new Media(url,
function()
{
console.log("playAudio():Audio Success");
},
function(err)
{
console.log("playAudio():Audio Error: "+err);
});
audio_file.play();
setTimeout(function() {
audio_file();
}, 5000);
}
Supported Platform
- Android
- iOS
- Windows Phone 7 ( Mango )
media.release

Sanjay SinghPosted Jun 22, 2014, 12:36 AM
good one..
talha haroonPosted Aug 1, 2012, 11:12 AM
thank you for your fine help I need to know that which VIDEO and AUDIO formats are allowed in phonegap? Please assist me
Ankit MittalPosted Feb 9, 2012, 6:34 AM
Article is helpful to know about media in PhoneGap