Audio in Haxe and NME

So we’ve already look at general development, graphics programming and handling input with Haxe with NME, now we are going to take a look at audio support.  This is by far going to be the shortest post of the series, as frankly, it’s the simplest.

 

Let’s jump right in with playing audio.

 

 

package gfs;    import nme.Assets;  import nme.display.Sprite;  import nme.Lib;  import nme.media.Sound;    class Main extends Sprite  {  	public function new()  	{  		super();  	    var song = Assets.getSound("audio/background.mp3");  		var soundfx1 = Assets.getSound("audio/effect2.wav");  		  		song.play();  		soundfx1.play();  	}  }  

 

 

Well, that was pretty straight forward.  There are only a few things to be aware of here.  First, notice we loaded the sounds using the Assets class with the path “/audio/something”.  This path needs to be defined in the NMML file.  Here is what mine looks like:

<assets path=”Assets/audio” rename=”audio” type=”audio” include=”*” />

The actual directory on your hard disk should be located within the folder Assets.  The next up thing to know is the file formats.  At the end of the day the supported formats are defined by the SDL_mixer library.  Notice how I used loaded the soundfx from a wav file, while the background music as an MP3?  There is a very important reason for this; you can only have one mp3 playing at a time.  Other more “short term” sound effects should be stored in a  lighter format.  Audio is going to be one of those tricky things, as different platforms can play different formats. It’s also important to realize that mp3 is a patent encumbered format, so if you are looking to sell your game, be careful with mp3, or they could come back to you looking for licensing fees!  The ogg vorbis format is a free alternative, but isn’t as widely supported.

 

Now let’s take a look at a slightly more advanced sample:

 

 

 

package gfs;    import haxe.Timer;  import nme.Assets;  import nme.display.Sprite;  import nme.events.Event;  import nme.Lib;  import nme.media.Sound;  import nme.media.SoundTransform;    class Main extends Sprite  {  	var playLeft = true;  	var song : Sound;  	  	public function new()  	{  		super();  	    song = Assets.getSound("audio/background.mp3");  		var soundfx1 = Assets.getSound("audio/effect2.wav");  		  		var soundChannel = song.play();  		  		// Call our sound effect every second... forever.  		new Timer(1000).run = function() {  			soundfx1.play();  		}  		  		soundChannel.addEventListener(Event.SOUND_COMPLETE, onSongEnd );  	}  	public function onSongEnd(event:Event) {  			Lib.trace("Fired");  			var channel = song.play();  			playLeft = !playLeft;  			  			if (playLeft)  				channel.soundTransform = new SoundTransform(1, -1);  			else  				channel.soundTransform = new SoundTransform(1, 1);    			channel.addEventListener(Event.SOUND_COMPLETE, onSongEnd );  		}  }  

 

 

This example is a bit more complex to show off a couple of the features of the audio libraries.  You may notice that play() returns a SoundChannel object.  This object has a number of useful features… you can manipulate the playback of the sound using this guy, by applying SoundTransform’s like we do here, to pan sound left or right, you can modify the volume, get current playback position, etc…  In this particular example, we load our two sound effects, start the background music playing, then create a timer that fires ever second, playing our second sound effect over and over and over.

We also wire up an event handler that will be fired when your soundChannel gets to the end ( SOUND_COMPLETE event ).  When that occurs, we toggle the sound to either play only on the left channel, or right channel.  We then recursively wire up our onSongEnd event on our newly created SoundChannel.  This code worked perfectly on every tested platform, although Android had some weird issues… it didn’t play properly at first, but once I lost and regained focus, it worked perfectly.

 

So, what about Video?

This is one area current NME is a bit lacking.  There are projects out there that provide native video playback on iOS as well as this project for playing back webm encoded video files, otherwise I believe you are currently out of luck.  So if you need to play cut screens, you are currently left rolling your own solution for each platform.  Fortunately, I do not need video support. 🙂


Scroll to Top