Skip to content

Instantly share code, notes, and snippets.

@macdonst
Created May 18, 2011 19:48
Show Gist options
  • Save macdonst/979385 to your computer and use it in GitHub Desktop.
Save macdonst/979385 to your computer and use it in GitHub Desktop.
PhoneGap Media Class Example
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>PhoneGap Back Button Example</title>
<script type="text/javascript" charset="utf-8" src="phonegap.0.9.5.js"></script>
<script type="text/javascript" charset="utf-8">
var myMedia = null;
var playing = false;
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";
document.getElementById('audio_position').innerHTML = "0.000 sec";
}
function onLoad() {
document.addEventListener("deviceready", onDeviceReady, false);
}
function onDeviceReady(){
console.log("Got device ready");
updateMedia();
}
function updateMedia(src) {
// Clean up old file
if (myMedia != null) {
myMedia.release();
}
// Get the new media file
var yourSelect = document.getElementById('playlist');
myMedia = new Media(yourSelect.options[yourSelect.selectedIndex].value, stopAudio, null);
// Update media position every second
var mediaTimer = setInterval(function() {
// get media position
myMedia.getCurrentPosition(
// success callback
function(position) {
if (position > -1) {
document.getElementById('audio_position').innerHTML = (position/1000) + " sec";
}
},
// error callback
function(e) {
console.log("Error getting pos=" + e);
}
);
}, 1000);
}
function setAudioPosition(position) {
document.getElementById('audio_position').innerHTML =position;
}
</script>
<body onload="onLoad()">
<h1>Audio Player</h1>
<p id="audio_position">0.000 sec</p>
<p>
<select id="playlist" onchange="updateMedia()">
<option checked value="/android_asset/www/test.mp3">Asset</option>
<option value="test.mp3">SD Card</option>
<option value="http://audio.ibeat.org/content/p1rj1s/p1rj1s_-_rockGuitar.mp3">Internet</option>
</select>
</p>
<a href="#" onclick="playAudio()"><img id="play" src="images/play.png"></a>
<a href="#" onclick="stopAudio()"><img id="stop" src="images/stop.png"></a>
</body>
</html>
@imagen7thh
Copy link

Hello, excellent work. Friend tested and works perfectly. I have only one problem with the vercion 1.5 SDK 'll know how you can work well?
css styles too well the working demo not vercion 1.5 that will be. vercion that is not supported? I'll be very grateful. regards

@macdonst
Copy link
Author

I have not tested this on Android 1.5. I wouldn't bother with that release unless you really need to. Most everyone has upgraded their phones to a more recent OS version.

@macdonst
Copy link
Author

macdonst commented Aug 11, 2011 via email

@Viktoru
Copy link

Viktoru commented Feb 24, 2013

I can't get it to work. I tried to add two different sound tracks in one simple screen app, nothing.... If I have two sound tracks play 1 and play 2, if play 1 is playing, it works fine but if I try to launch the second track, it won't play.

@gavy81
Copy link

gavy81 commented Mar 10, 2013

Hi Simon, I somehow made it work with .mp3 and tested your example, works awesome, but in certain cases, i do have playlist files whic are in .smil format and .wvx format, i opened those files in the notepad and found some links to the external ip address where the file of format .asf were there, now i am in jeopardy how do i make that .wvx or .smil playlist work with this code, so it should automatically find the source inside them and starting playing. i am stuck, else work it works fine with example you gave. here is the site which i am talking about

http://tinyurl.com/bl8otph

i used the ripple emulator and you can even check the website there

@neroze
Copy link

neroze commented Jun 2, 2013

Hello,

I am have some problem with online radio streaming... It works fine for remote audio file but I am having problem with embedding online radio streaming. Can you please suggest me anything that I am missing ??

@mrfatguy
Copy link

For those, who has just googled this gist, here is Simon's blog post with nearly every line of above code gently explained. Thank you, Simon, great job, as usual, all thumbs up! :]

@bayomio
Copy link

bayomio commented May 8, 2014

Thank You Si. Would this work with the latest Android OS? Many thanks again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment