How to play sound when a button is pressed in an Android application built using PhoneGap / Cordova?

I am creating a basic sound application that has several buttons that play sounds. I use PhoneGap / Cordova and I use the PhoneGap Build service to compile the code in the APK. At first, I used simple HTML5 tags <audio>that worked perfectly in the desktop browser, as well as in the Android browser, but when they were packaged in the PhoneGap application, no sounds were played.

I looked around and saw some mention of the need to use the Media API in PhoneGap instead of the standard HTML5 sound, so I switched to it with the following code changes.

I have the following function:

function doPlay(soundId) {
  var my_media = new Media("/android_asset/www/"+soundId+".mp3",
    function() {
      navigator.notification.alert('Success!', alertDismissed);
    },
    function(err) {
    navigator.notification.alert('Error!', alertDismissed);
  });
  my_media.play();
}

Each button has a handler onclicksimilar to this ...

onclick="doPlay('sound1');"

, index.html, sound1.mp3, sound2.mp3 .. alertDismissed() , .

. . 'Success' 'Error'. , notification media.

. ?

. @simon-macdonald, ( "/android_asset/www/" ), , . , /.: - (

+3
2

. :

var my_media = new Media("/android_asset/www/" + soundId+".mp3",
function() {
  navigator.notification.alert('Success!', alertDismissed);
},
function(err) {
navigator.notification.alert('Error!', alertDismissed);
});
my_media.play();

-.

http://simonmacdonald.blogspot.ca/2011/05/using-media-class-in-phonegap.html

+3

, , , , , java- cordova .

<script type="text/javascript" charset="utf-8" src="cordova-1.7.0.js"></script> 

.

0

All Articles