admin管理员组

文章数量:1418038

I'm building a basic soundboard-like app which has a few buttons that play sounds. I'm using PhoneGap/Cordova and I'm using the PhoneGap Build service to pile the code to APK. At first, I was using simple HTML5 <audio> tags which worked fine in the desktop browser as well as the Android browser, but when packaged into the PhoneGap app, no sounds were being played.

I looked around and saw some mentions of having to use the Media API in PhoneGap instead of standard HTML5 audio, so I switched to that with the following code changes.

I have the following function in place:

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 an onclick handler similar to this...

onclick="doPlay('sound1');"

The files are in the same directory as the index.html, called sound1.mp3, sound2.mp3 and so on. And the function alertDismissed() has also been defined, but it's empty.

However, it still does not work. There is no sound on click. There is no 'Success' or 'Error' message either. Yes, I've got notification and media permissions requested.

I'm quite stumped here. Any ideas?

Update: As suggested by @simon-macdonald, I put a full path to the local files ("/android_asset/www/"), but that still hasn't gotten the app to work. No sound, no error/success alerts. :-(

I'm building a basic soundboard-like app which has a few buttons that play sounds. I'm using PhoneGap/Cordova and I'm using the PhoneGap Build service to pile the code to APK. At first, I was using simple HTML5 <audio> tags which worked fine in the desktop browser as well as the Android browser, but when packaged into the PhoneGap app, no sounds were being played.

I looked around and saw some mentions of having to use the Media API in PhoneGap instead of standard HTML5 audio, so I switched to that with the following code changes.

I have the following function in place:

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 an onclick handler similar to this...

onclick="doPlay('sound1');"

The files are in the same directory as the index.html, called sound1.mp3, sound2.mp3 and so on. And the function alertDismissed() has also been defined, but it's empty.

However, it still does not work. There is no sound on click. There is no 'Success' or 'Error' message either. Yes, I've got notification and media permissions requested.

I'm quite stumped here. Any ideas?

Update: As suggested by @simon-macdonald, I put a full path to the local files ("/android_asset/www/"), but that still hasn't gotten the app to work. No sound, no error/success alerts. :-(

Share Improve this question edited Apr 29, 2012 at 12:06 aalaap asked Apr 24, 2012 at 11:46 aalaapaalaap 4,4115 gold badges56 silver badges62 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

The path to you files is not correct. You want to do:

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();

Check out my mini tutorial.

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

I have a feeling your problem is not related to the code you've written, but just maybe you have forgotten to include the cordova javascript file in your head section.

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

Best of luck.

本文标签: