Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phonegap Media API (Android) - Media is not defined

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?

like image 992
user1328173 Avatar asked Apr 17 '12 06:04

user1328173


1 Answers

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>
like image 162
Devgeeks Avatar answered Oct 24 '22 00:10

Devgeeks