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 badges2 Answers
Reset to default 3The 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.
本文标签:
版权声明:本文标题:javascript - How do I play audio on clicking a button in an Android app built using PhoneGapCordova? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745283381a2651557.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论