Tuesday, June 07, 2011

Sound with SoundManager2

HTML5 audio doesn't seem to be much use as of yet, and I've been advised to use SoundManager2 as my audio library for the HTML5 version of Creatures & Castles.
For those of you unfamiliar with the library, it's basically a Javascript to Flash bridge wrapper that uses a small Flash application to handle playing sounds.
Note that it does have beta support for HTML5 audio but, honestly, it's not worth using (yet). Besides, why bother with a wrapper library such as this if you're just going to tell it to use HTML5 Audio anyway.

In any case, the API is fairly straightforward (although the reference documentation can be a little obtuse). It seems to be a fairly low level library; there is no built in cross-fade or anything like that (at least not that I saw), but it does perform its basic job admirably.
That is, it will load sounds and play them in an effective cross-platform manner.

There is one caveat I ran into that I think I will cover here. In many cases in the game, I want to be able to simultaneously play multiple copies of the same sound. There is a sound object property that can be set for this (autoShot, I believe), but with the default initialization parameters for the soundManager object this has no useful effect. In order to be able to play multiple copies of the same sound (without actually physically loading multiple copies), you have to explicitly set the soundManager.flashVersion property to 9 (as opposed to the default value of 8) in the SoundManager2 initialization section.

It took me a while to figure this one out as, although it is in the documentation, it's not explicitly obvious. Hopefully this will save some time for others with the same problem.
Post a Comment