4

I am working on an app and using IntelXDK to build it. I need to play some sounds if conditions are met, first I have tried HTML5 with JS and on desketop it's working but when built, it has no sound...

First attempt - HTML5 & JS

<audio src="snd/yes.mp3" id="yes"></audio>
<audio src="snd/no.mp3" id="no"></audio>

if(condition) {
    $('#yes').trigger("play");
} else {
    $('#no').trigger("play");
}

Then I tried the native IntelXDK version that goes like this:

if(condition) {
    intel.xdk.player.playSound("snd/yes.mp3");
} else {
    intel.xdk.player.playSound("snd/no.mp3");
}

No only that it doesn't work but it also f***s up the rest of my code not allowing the popup and page change to trigger.

Does anyone know how to fix this ? Do I need to preload the sounds before playing them?

**UPDATE

I just discovered that if I use the HTML5 Audio tags and I give links to the sounds inside my server, the sounds are working, but if I try to get the same sounds from my snd folder they won't work...why is that?

Alin
  • 1,218
  • 5
  • 21
  • 47
  • Are you getting any error? If the code above stops other functions from running then there is a syntax error somewhere. – Omar Dec 14 '14 at 18:20
  • @omar , no syntax error no nothing, like I said, the entire app works fine on the browser but it's not working when built for android. Second version is taken from the Intel website as a solution but they often give solutions that don't work... – Alin Dec 14 '14 at 18:54
  • 1
    You can use ion plugging if you wish. I tested this one and its pretty good. http://ionden.com/a/plugins/ion.sound/en.html – Tasos Dec 14 '14 at 19:50

1 Answers1

1

The problem is that your "root" is not where you think it is, so your "snd" directory is not being found. This problem varies, unfortunately, with each target, it is not something the XDK controls, it has to do with how Cordova apps are constructed. You can use these functions to locate the root of your files and that should help you make this work.

// getWebPath() returns the location of index.html
// getWebRoot() returns URI pointing to index.html

function getWebPath() {
    "use strict" ;
    var path = window.location.pathname ;
    path = path.substring( 0, path.lastIndexOf('/') ) ;
    return 'file://' + path ;
}

function getWebRoot() {
    "use strict" ;
    var path = window.location.href ;
    path = path.substring( 0, path.lastIndexOf('/') ) ;
    return path ;
}
xmnboy
  • 2,314
  • 2
  • 14
  • 31
  • I wish that was the problem, but I doubt it...since I have an img folder next to the snd folder and whenever I call an image the same as when I call a sound, the image displays. – Alin Dec 15 '14 at 19:44
  • When you "call" the image do you mean you are using an tag or are you actually calling an intel.xdk API? These are not equivalent. Also, have you checked out the audio demo apps in the XDK? See the "Basic Hybrid" app. – xmnboy Dec 16 '14 at 20:27