Sound Object

32 views
Skip to first unread message

Paul Tomlinson

unread,
Mar 22, 2012, 1:03:53 PM3/22/12
to Jangaroo Users
Dear All,

Is there a trick to loading an mp3? The following code just returns
'error [object Event]' in the browser.....

var req:URLRequest = new URLRequest("sounds/PeekabooLoop_Master.mp3");
var snd:Sound = new Sound();
snd.load(req);
sn.play();

Thanks in advance,

Paul

Frank

unread,
Mar 22, 2012, 4:31:48 PM3/22/12
to Jangaroo Users
This should work. Most likely, the sound is not found, maybe you
expect
the relative URL to be resolved against another base URL than it is
actually resolved. The relative URL is set as the "src" of a generated
HTML5 <audio> element, so it has to be relative to the URL of the
surrounding HTML page.
Also make sure that the browser can play mp3s, and that the server
uses the correct content type.
You could try a static HTML5 test page using <audio src="sound/..."/>
and see if that works. If it doesn't, you have another, more basic
problem
that does not have to do with Jangaroo at all.

If all this does not work, there is an alternative way, the way Flixel
does it,
which I made work with Jangaroo, which is to embed the sound, like so:

package foo.bar {

public class Player {
[Embed(source="../../data/hit.mp3")]
protected var SndHit:Class;

public function createAndPlay():void {
var sound:Sound = new SndHit();
sound.play();
}
}
}

Note the relative path: the two ../ are to navigate from "foo.bar"
back to the root package, where we have a top-level folder
"data" holding the sound files. So the example sound file should be
placed in src/main/joo/data/hit.mp3 (for build process / deployment,
see below).
Because some browsers (Firefox?) do not play mp3s, Jangaroo
tries to load an ogg sound from the same base URL as a fallback.
You can use some tool like Audacity to produce oggs from your
mp3s.

When building via Maven, you have to tell Maven in your pom.xml
to deploy the sound resources to the correct location in the Web-app,
in case of [Embed] this is the sub-path joo/classes:

<resources>
<resource>
<directory>src/main/joo</directory>
<includes>
<include>**/*.png</include>
<include>**/*.mp3</include>
<include>**/*.ogg</include>
<include>**/*.ttf</include>
<include>**/*.txt</include>
</includes>
<targetPath>joo/classes/</targetPath>
</resource>
</resources>

Find a fully-working example at
https://github.com/fwienber/Mode
Note that there, sounds are played through the helper class
FlxSound (which you'd find in https://github.com/fwienber/flixel),
but it basically delegates to flash.media.Sound. So the tricky
part is where to put the sound files and how the annotated
field has to look like.

Good luck!

Paul Tomlinson

unread,
Mar 23, 2012, 9:55:55 AM3/23/12
to Jangaroo Users
Hi Frank,

Thanks for this!

Unfortunately, this method doesn't work on iOs so I've successfully
added SoundManager2 functionality to my onClick events.

http://www.schillmania.com/projects/soundmanager2/

Thanks,

Paul
> Find a fully-working example athttps://github.com/fwienber/Mode
> Note that there, sounds are played through the helper class
> FlxSound (which you'd find inhttps://github.com/fwienber/flixel),

Frank

unread,
Mar 23, 2012, 10:27:43 AM3/23/12
to Jangaroo Users
Right, soundmanager2 tries hard to work-around the very
broken iOS <audio> support. If you want to access soundmanager2
from Jangaroo through a typed ActionScript API, check out the
Jangaroo Libs integration:
https://github.com/CoreMedia/jangaroo-libs/tree/master/soundmanager2
Maybe you have to update the actual JS code, but the API should
not have changed much, so the ActionScript API should be okay.

How did you solve the problem that on iOS, audio may only be played
on a user interaction?

Paul Tomlinson

unread,
Mar 30, 2012, 11:17:40 AM3/30/12
to Jangaroo Users
Thanks Frank!

SoundManager worked ok for me via a browser, but not for iOS. I shall
check out the SM from Jangaroo though!

I eventually used the Media API in Phonegap, which is great.

In terms of iOS user interaction, I just built a simple game & it
wasn't a problem....click to start game, click to select etc...

Paul
Reply all
Reply to author
Forward
0 new messages