I need your advice about game sounds.
I have about 100 short sounds and about 5 long music tracks. I combine them in .fla file and compile .swf file.
In my AIR game I dynamically load .swf and use it such way:
var appDomain:ApplicationDomain = null;
for each(appDomain in _applicationDomainArray)
var temp:Class = appDomain.getDefinition(name) as Class;
return new temp() as Sound;
What can your recommend in OpenFL/Starling version of game?
Is it a good idea to embed about 100 .ogg files in project.xml?
How do your use sounds in your projects?
For sound effects, it’s probably fine.
For music tracks, it depends on how long the tracks are and when you choose to load them. I’ve found that OpenFL takes a while to load .ogg music files and that can cause hiccups in the CPU. We were loading music tracks in our OpenFL game (targeting a Windows .exe) as things were animating and there was always about a half second stutter.
If you can stomach using more disk space, .wav files will load very quickly. Otherwise, you might have to try loading the .ogg files on a separate thread.
What mechanism do you use to load .ogg/.wav files?
embed=true in project.xml
or anything else?
I use Starling’s AssetManager with .ogg files
assetManager.enqueueWithName(Config.PATH_MUSIC + fileName + fileExtension, musicID);
private function callbackFunction():Void
trace("music files loaded");
What about formats? Safari does’t support .ogg
How to configure OpenFL/Starling project to use different formats for one game sound?
I use conditionnal compiling or whatever it’s called
fileExtension = ".mp3";
fileExtension = ".ogg";
I don’t have much web experience with openFL, if there is a format that will work with all browsers (mp3 maybe ?) then you can do
fileExtension = ".mp3";
Otherwise you will probably have to mix conditionnal compiling + safari detection
I think there is a mechanism inside Lime or OpenFL to automate that.
Like using html5 ‘audio’ tag:
<source src="myAudio.mp3" type="audio/mpeg">
<source src="myAudio.ogg" type="audio/ogg">
There might be an even better way, but I know of a couple of options (Note: my examples will use OpenFL’s asset system, rather than Starling’s).
project.xml, give each sound asset an
id, but use a different file extension for different targets:
<assets path="sounds/my_sound.mp3" id="sounds/my_sound" if="html5"/>
<assets path="sounds/my_sound.ogg" id="sounds/my_sound" if="cpp"/>
Since they both have the same
id, the Haxe code shouldn’t need any conditional compilation:
var sound:Sound = Assets.getSound("sounds/my_sound");
If you have a lot of sounds, it might be somewhat tedious to list every individual sound file in your
project.xml, so you could include all sounds with a particular file extension like this:
<assets path="sounds" include="*.mp3" if="html5"/>
<assets path="sounds" include="*.ogg" if="cpp"/>
Then, you need only minor conditional compilation in your Haxe code to differentiate between extensions:
private static final SOUND_FILE_EXTENSION:String = #if html5 ".mp3" #else ".ogg" #end;
Which you’d use like this:
var sound:Sound = Assets.getSound("sounds/my_sound" + SOUND_FILE_EXTENSION);
But how about one html5 target with different extensions?
.ogg for all browsers except Safari and .mp3 for Safari?
That kind of detection would need to be done at run-time rather than compile-time. I don’t think OpenFL has an API for that.
However, here’s one way to do it manually from Haxe by calling native JS APIs:
var htmlWindow = cast(js.Lib.global, js.html.Window);
var audio = cast(htmlWindow.document.createElement("audio"), js.html.Audio);
var canPlayOgg = audio.canPlayType("audio/ogg; codecs=vorbis") != "";
trace("can play ogg? " + canPlayOgg);
If you plan to support native targets too, you’ll obviously need to wrap that section in
#if html5 and
On which target ? Pretty sure I used .wav on windows with no issue. If it’s a Starling issue I’ll look into it
My experience with web is limited/old but I think all browsers do this and there’s no way around it ? Kinda like fullscreen, I think you have to make the user click or tap something in your app first (I could be wrong)
On which target ?
_assets = new AssetManager(1);
Assets.getPath("assets/sounds/village_loop.ogg"), // no errors
Assets.getPath("assets/sounds/ambar.wav") // error
private function onError(str:String):Void
// str = "Unknown error" for ambar.wav
File ambar.wav (if you need)
I only did a quick test on getting the path but it works for me : could it be something with your project.xml asset settings ?
if you look into your html5/bin/assets/sounds folder, is the ambar.wav file there ?
<assets path="assets/sounds" embed="false" />
I converted .wav to .mp3 and now it works.
About my second question. I added temporal global listener to first TouchEvent from user to start music. I don’t know is it a good solution, but it works.
P.S. If it is interested - my current game working progress -
My bad, I thought you were having an error when trying to get the path
I’ll do some further checking on this.
The game looks good already, I was lucky to find the button to change the language
I love how russian looks but I can’t read anything ^^
I got some mushrooms but didn’t really understand what I was supposed to do
By the way, you might be interested in knowing that there is a port of as3 feathers for openFL-starling in progress. I don’t know how much time it will take though.
It is only part of game) It will be “city builder” with mushrooms collecting as economic part of game.
“feathers for openFL-starling” sounds interesting! As for me I need reach textField stuff form it.