I want to build a Android WebView app, which plays a sound when the user presses a button.For some reason I can't get the PhoneGap media API to work.
It keep showing:
Uncaught ReferenceError: Media is not defined at
file:///android_asset/www/script.js:3
Here is my code:
$(document).ready(function(){
var myMedia = new Media("/android_asset/www/one.mp3");
$('.one').click(function(){
myMedia.play();
});
});
Anyone know how to fix this problem?
You might be trying to call Media
before the deviceready event fires.
<script type="text/javascript">
var myMedia = null;
document.addEventListener("deviceready",onDeviceReady,false);
function onDeviceReady () {
myMedia = new Media("/android_asset/www/one.mp3",
function(){
if (myMedia) {
myMedia.stop();
myMedia.release();
}
},
function(error){
console.log(error.message);
}
);
}
$(document).ready(function(){
$('.one').click(function(event){
myMedia.play();
});
});
</script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With