0

i am trying to load a sound and play it when two nape bodies collide i have the collision detection working but cant play the sound the error is coming from the following code:

var collisionSound:Sound;


collisionSound = new Sound();
collisionSound.addEventListener(Event.COMPLETE, onSoundLoaded);
collisionSound.load(new URLRequest("bang.mp3"));


private function onSoundLoaded(e:event):void
{
    collisionSound.play();
}

i have sample code in another script very similar to this thaat works help would be greatly appreciated.

Lonergan6275
  • 1,938
  • 6
  • 32
  • 63

1 Answers1

5

Did you try playing it after it is loaded? Also add error handlers to see if there are any errors while loading the file.

collisionSound = new Sound();
collisionSound.addEventListener(Event.COMPLETE, onSoundLoaded);
collisionSound.addEventListener(IOErrorEvent.IO_ERROR, onSoundLoadError);
collisionSound.load(new URLRequest("bang.mp3"));


private function onSoundLoaded(e:Event):void
{
   collisionSound.play();
}

private function onSoundLoadError(e:IOErrorEvent):void
{
   trace(e.text);
}
Barış Uşaklı
  • 13,440
  • 7
  • 40
  • 66
  • yes i did with no joy i alos put a trace in the onSoundLoaded function that is not exacuting – Lonergan6275 Mar 27 '13 at 16:05
  • If `onSoundLoaded` is not executing your file isn't loading properly. Try adding an error listener and see what is going on. `sound.addEventListener(IOErrorEvent.IO_ERROR, onSoundIOError);` – Barış Uşaklı Mar 27 '13 at 16:07
  • and what goes in the handler function and i presume its collisionSound rather than sound – Lonergan6275 Mar 27 '13 at 16:09
  • Edited my answer, you can trace the error message and see what is wrong. – Barış Uşaklı Mar 27 '13 at 16:10
  • Error #2032: Stream Error. URL: app:/bang.mp3 is the error the sound file is in the same folder as the class – Lonergan6275 Mar 27 '13 at 16:12
  • 2
    You might need to place the mp3 file next to the swf file. – Barış Uşaklı Mar 27 '13 at 16:16
  • 2
    Barış Uşaklı is right. All urls that do not include a protocol (http://, app://, etc) are relative to the SWF file. So you need to place the mp3 in the same dir as your SWF and point it to that location (i.e. if you put a folder title "sounds" in your SWF directory, the url would be `sounds/sound.mp3`) – Josh Mar 27 '13 at 16:19
  • thanks very much guys i was publishing air for android and set the destination to desktop for convenience of installing an my nexus 7. thanks again. – Lonergan6275 Mar 27 '13 at 16:29