-
-
Save alunny/2380994 to your computer and use it in GitHub Desktop.
<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> |
I tried this code with Phongap Build and an Android Device, i become the error "undefined"!! Do i have to change the path when i use Phonegap Build??
Aha. Thanks so much Andrew. This works for me in a small audio-book app i'm starting to develop. Brilliant. (I've tried 5-6 methods and this is the one that works.)
What I'm wondering next is whether i can start and stop the audio file playing at 2 different points. I've seen some code suggesting i can start at a certain point... would you have any pointers or links on this?
Thank. =)
@mickleberry: Just search the web! :] Here you have a complete example -- finding it took me just a few seconds. Cheers! :]
THANK YOU!! I've been looking for so long for an example code that works!!
it's work only in the ripple emulator bu on the mobile doesn't work. can you help me please in this ?
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.)
Thanks...the sound works really fine in android. Can you give me suggestion on how can i stop playing the sound?
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!
Finally I found something that works after ages of searching the net 😄
You're a life saver man,
thanks, can you include a stop and pause function. I tried....didn't works....this code works on Android but not IOS