Skip to content

Instantly share code, notes, and snippets.

@alunny
Created April 13, 2012 23:37
Show Gist options
  • Save alunny/2380994 to your computer and use it in GitHub Desktop.
Save alunny/2380994 to your computer and use it in GitHub Desktop.
play local audio
<html>
<h1>Playing Audio</h1>
<button onclick="playAudio('brobob.mp3')">Play Some Audio</button>
<script src="phonegap.js"></script>
<script>
function playAudio(src) {
if (device.platform == 'Android') {
src = '/android_asset/www/' + src;
}
var media = new Media(src, success, error_error);
media.play();
}
function success() {
// ignore
}
function error_error(e) {
alert('great error');
alert(e.message);
}
</script>
</html>
@davidwirth
Copy link

I couldn't get it to work until I eventually figured out that the device.platform reference requires the "device" plugin. (At least it finally works on Android -- haven't tested iOS yet.)

@GKomala
Copy link

GKomala commented May 13, 2014

Thanks...the sound works really fine in android. Can you give me suggestion on how can i stop playing the sound?

@coolwebs
Copy link

For people wanting some more playback options, check out Simon McDonald's tutorial out @ http://simonmacdonald.blogspot.com.au/2011/05/using-media-class-in-phonegap.html

I adapted the code and it worked a treat for me!

@Eskander
Copy link

Eskander commented Jun 9, 2015

Finally I found something that works after ages of searching the net 😄
You're a life saver man,

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