The example will be a basic Media player with play/pause and stop functionality. Here's a quick screen shot:
On Android you can create a Media object using one of three ways. The first way is referencing a file in your "android_asset" directory. For instance if you have a file called "test.mp3" in your projects "assets/www" directory you would create a new media object by doing:
myMedia = new Media("/android_asset/www/test.mp3");The second way to create a media object is by referencing a file on your devices file system. Say for instance, you have a file at "/sdcard/test.mp3" of your device you would create the media object by doing:
myMedia = new Media("test.mp3");You'll notice the "/sdcard/" portion of the file name is omitted. This is because on Android the Media object makes an assumption that the file is stored on your "/sdcard". Note: I may change this behaviour in an upcoming version of PhoneGap but I'll strive to retain backward compatibility.
The third and final way to create a media object is by URL:
myMedia = new Media("http://audio.ibeat.org/content/p1rj1s/p1rj1s_-_rockGuitar.mp3");Now that we've got our media object we want to be able to do the sensible thing and be able to play the audio. For that we'll create a play button:
<a href="#" onclick="playAudio()"><img id="play" src="images/play.png" /></a>and corresponding function:
function playAudio() {
myMedia.play();
}Now the stop button is just as simple:<a href="#" onclick="stopAudio()"><img id="stop" src="images/stop.png" /></a>
function stopAudio() {
myMedia.stop();
}However, that's pretty lame as it would be great to be able to pause the audio playback so let's enhance out play and stop functions:function playAudio() { if (!playing) { myMedia.play(); document.getElementById('play').src = "images/pause.png"; playing = true; } else { myMedia.pause(); document.getElementById('play').src = "images/play.png"; playing = false; } } function stopAudio() { myMedia.stop(); playing = false; document.getElementById('play').src = "images/play.png"; }Now when the user is playing some audio the play button image is changed to a pause button and hitting the pause button again will, well, pause the playing of the audio. I'm cheating a little bit by using a global variable called playing as it could easily be changed to check what the currently set image is and doing the correct action.
Of course it is always nice to know where we are at in a given audio clip so we can add some functionality to get the current position of where we are at in the file. So let's create a function that will run every second and update our HTML with the current position of the audio file.
<p id="audio_position">0.000 sec</p> var mediaTimer = setInterval(function() { myMedia.getCurrentPosition( function(position) { if (position > -1) { document.getElementById('audio_position').innerHTML = (position/1000) + " sec"; } }, function(e) { console.log("Error getting pos=" + e); } ); }, 1000);
That's all well and good but there is one thing that's slipped by our notice so far. When an audio clip finishes playing the pause button should become a play button in case the user wants to play the clip again. That can be accomplished by adding a success call back to our Media object constructor. The success call back is executed when the clip has finished playing. So we'll now instantiate our media like this:
myMedia = new Media("/android_asset/www/test.mp3", stopAudio);So what we have here are the beginnings of a audio player application for Android. In part two of this article I'll enhance the app with the File System and Directory API showing you how to scan your device for audio files.
The HTML and images can be found here and below is a listing of the HTML code uses in this article. I added a select box so that users could switch between the different ways of creating a Media object. You'll have to supply your own MP3 file as I don't want the RIAA coming after me.



