3

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 compile 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. :-(

4

2 に答える 2

3

ファイルへのパスが正しくありません。あなたがしたい:

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

于 2012-04-24T14:54:11.207 に答える
0

あなたの問題はあなたが書いたコードとは関係がないように感じますが、コードバ JavaScript ファイルを head セクションに含めるのを忘れているのかもしれません。

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

幸運を祈ります。

于 2012-05-30T19:19:46.147 に答える