Introduction
We can record and play audio files on a device using the Media object. It takes the four parameters:
src: It represents the URL that contains the audio content.
mediaSuccess: It is optional and invoked after the media onject has completed the current play/record and stop action.
mediaError: When the error has occured then this is invoked.
mediaStatus: It indicate the status changes.
Methods in Media
media.getCurrentPosition: Returns the current position within the audio file. It takes two parameters; the first is the mediaSuccess and the second is the mediaError. The supported platforms are Android, BlackBerry WebWorks (OS 5.0 and higher), iOS and Windows Phone 7 (Mango).
Example
// Audio player
var my_media = new Media(src, onSuccess, onError);
// Update media position every second
var mediaTimer = setInterval(function () {
// get media position
my_media.getCurrentPosition(
// success callback
function (position) {
if (position > -1) {
console.log((position) + " sec");
}
},
// error callback
function (e) {
console.log("Error getting pos=" + e);
}
);
}, 1000);
media.getDuration: Returns the duration of the audio file. It is a synchronous function that returns the duration of the sudio file if the file is known if unknown then return -1. The supported platforms are Android, BlackBerry WebWorks (OS 5.0 and higher), iOS and Windows Phone 7 (Mango).
Example
// Audio player
var my_media = new Media(src, onSuccess, onError);
// Get duration
var counter = 0;
var timerDur = setInterval(function () {
counter = counter + 100;
if (counter > 2000) {
clearInterval(timerDur);
}
var dur = my_media.getDuration();
if (dur > 0) {
clearInterval(timerDur);
document.getElementById('audio_duration').innerHTML = (dur) + " sec";
}
}, 100);
media.pause: It will pause the playing audio files. The function media.pause is a synchronous function that pauses any audio files that are playing. The supported platforms are Android, BlackBerry WebWorks (OS 5.0 and higher), iOS and Windows Phone 7 (Mango).
Example
// Play audio
function playAudio(url) {
// Play the audio file at url
var my_media = new Media(url,
// success callback
function () {
console.log("playAudio():Audio Success");
},
// error callback
function (err) {
console.log("playAudio():Audio Error: " + err);
});
// Play audio
my_media.play();
// Pause after 10 seconds
setTimeout(function () {
media.pause();
}, 10000);
}
media.play: Starts or resumes an audio file. It is a synchronous function. The supported platforms are Android, BlackBerry WebWorks (OS 5.0 and higher), iOS and Windows Phone 7 (Mango).
Example
// Play audio
function playAudio(url) {
// Play the audio file at url
var my_media = new Media(url,
// success callback
function () {
console.log("playAudio():Audio Success");
},
// error callback
function (err) {
console.log("playAudio():Audio Error: " + err);
});
// Play audio
my_media.play();
}
media.release: Releases the underlying operating system audio resources. It is a synchronous function. This is particularly important for Android applications. The supported platforms are Android, BlackBerry WebWorks (OS 5.0 and higher), iOS and Windows Phone 7 (Mango).
Example
// Audio player
var my_media = new Media(src, onSuccess, onError);
my_media.play();
my_media.stop();
my_media.release();
media.seekTo: Sets the current position within an audio file. It is an asynchronous function that updates the curtrent position of the audio file. It will take a parameter called milliseconds that defines the playback position within the audio in milliseconds. The supported platforms are Android, BlackBerry WebWorks (OS 5.0 and higher), iOS and Windows Phone 7 (Mango).
Example
// Audio player
var my_media = new Media(src, onSuccess, onError);
my_media.play();
// SeekTo to 10 seconds after 5 seconds
setTimeout(function () {
my_media.seekTo(10000);
}, 5000);
media.startRecord: Records an audio file. It is a synchronous function. The supported platforms are Android, BlackBerry WebWorks (OS 5.0 and higher), iOS and Windows Phone 7 (Mango).
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();
}
media.stop: Stops the audio file; it is a synchronous function. The supported platforms are Android, BlackBerry WebWorks (OS 5.0 and higher), iOS and Windows Phone 7 (Mango).
Example
// Play audio
function playAudio(url) {
// Play the audio file at url
var my_media = new Media(url,
// success callback
function () {
console.log("playAudio():Audio Success");

Comments
Join the conversation! Your thoughts help the community grow.