Working on a basic sound board for a trip abroad. Set up everything to work fine in Browsers but wanted to use PhoneGap Build/GitHub to make it downloadable as cell service won't be available.
Everything working great in desktop browser however once its installed on Android, the mp3 files don't play. I've read some others posts on here about the inconsistencies of mp3s on PhoneGap. Some suggestions offered using "media" in place of . I'm not sure how to swap that out in place of the code I have now. Seems to use javascript instead of just html. http://docs.phonegap.com/en/2.0.0/cordova_media_media.md.html#Media Sample of what I'm using:
<audio id="yes" src="audio/basic/yes.mp3"></audio>
<audio id="no" src="audio/basic/no.mp3"></audio>
<button class="button" onclick="document.getElementById('yes').play()">Yes</button>
<button class="button" onclick="document.getElementById('no').play()">No</button>
Works great in browser, but not on Android. Can anyone offer a starting point to correct the audio?
Thanks
-BR
EDIT 2-9-14
No luck so far. The media plugin is showing as correctly installed and cordova.js is included. Here's what I've tried.
<!DOCTYPE HTML>
<html>
<head>
<title>Media Example</title>
<link rel="stylesheet" type="text/css" href="style.css">
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">
function playAudio(url) {
// Play the audio file at url
var my_media = new Media(url,
// success callback
function() {
console.log("playAudio():Audio Success");
},
// error callback
function(err) {
console.log("playAudio():Audio Error: "+err);
});
// Play audio
my_media.play();
}
</script>
</head>
<body>
<a href="#" class="button" onclick="playAudio('/android_asset/www/audio/basic/yes.mp3');">Yes</a>
</body>
</html>
Also tried to work with the code you suggested
<!DOCTYPE html>
<html>
<head>
<title>Transactions</title>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript">
var my_media = null;
var mediaTimer = null;
function playAudio(src) {
// Create Media object from src
my_media = new Media(src, onSuccess, onError);
// Play audio
my_media.play();
// Update my_media position every second
if (mediaTimer == null) {
mediaTimer = setInterval(function () {
// get my_media position
my_media.getCurrentPosition(
// success callback
function (position) {
if (position > -1) {
setAudioPosition((position) + " sec");
}
},
// error callback
function (e) {
console.log("Error getting pos=" + e);
setAudioPosition("Error: " + e);
});
}, 1000);
}
}
// onSuccess Callback
function onSuccess() {
console.log("playAudio():Audio Success");
}
// onError Callback
function onError(error) {
alert('code: ' + error.code + '
' +
'message: ' + error.message + '
');
}
</script>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
document.addEventListener("deviceready", function () {
$("#Yes").on("click", function () {
playAudio(audio/basic/yes.mp3);
});
$("#No").on("click", function () {
playAudio(audio/basic/no.mp3);
});
<button class="button" id="Yes">Yes</button>
<button class="button" id="No">No</button>
</body>
</html>
Edit 2/10/14
Unfortunately still no such luck. Most recent update below
<!DOCTYPE html>
<html>
<head>
<title>Transactions</title>
<!-- JavaScript -->
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript">
document.addEventListener("deviceready", function () {
$("#yes").on("click", function () {
playAudio(/android_asset/www/audio/basic/yes.mp3);
});
$("#no").on("click", function () {
playAudio(/android_asset/www/audio/basic/no.mp3);
});
}, false);
var my_media = null;
var mediaTimer = null;
function playAudio(src) {
// Create Media object from src
my_media = new Media(src, onSuccess, onError);
// Play audio
my_media.play();
}
// onSuccess Callback
function onSuccess() {
console.log("playAudio():Audio Success");
}
// onError Callback
function onError(error) {
alert('code: ' + error.code + '
' +
'message: ' + error.message + '
');
}
</script>
<!-- CSS -->
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<button class="button" id="yes">Yes</button>
<button class="button" id="no">No</button>
</body>
</html>
See Question&Answers more detail:
os