Alex Jeffery

HomeArchivesAboutFeed

How To Embed An MP3 In Haxe With FlashDevelop

Another quick tutorial about using embedded assets in Flash using Haxe and Flashdevelop.
This time we are embedding an mp3.

For some reason in FlashDevelop after adding an mp3 to your project you can’t add the mp3 to the library. I am unsure why this is.  What this means is we can’t embed the mp3 in exactly the same way as we embed a bitmap

Luckly there is a work around. There is a utility program called SWFMill that can create SWF files based on an xml config file. It looks like FlashDevelop uses SWFMill behind the scenes to create it’s library. You can see this by looking in the obj folder for a flash project as there is an xml file in there and also in the FlashDevelop install location, it includes a version of SWFMill.

So the first step is to create a swf file that contains our target mp3 file. My example xml config looks like this.

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <movie version="9" width="800" height="600" framerate="30">
  3.   <background color="#FF0080" />
  4.   <frame>
  5.     <library>
  6.       <sound id="Funk" name="Funk" import="Funk.mp3"/>
  7.     </library>
  8.   </frame>
  9. </movie>

After creating the config file we call SWFMill to build the swf file with our mp3 in it. The  call looks like this.

swfmill.exe simple config.xml music.swf

After SWFMill has run you should get a music.swf file and it should be about the same file size as your mp3.

Now import the music.swf file into your FlashDevelop project.
Right click the music.swf file and select the add to library option.

Now we need to create a class for the music file. There is one thing to note here. If you want this music class to be in a package you must set it’s name attribute in the SWFMill config above to “packageName.AssetName” or else you will get an error 2068 invalid sound. Here is the code snippet to create the sound class.

  1. import flash.media.Sound;
  2. class Funk extends Sound { public function new() { super(); } }

Playing the music is now very easy.

  1. var music:Funk;
  2. music = new Funk();
  3. music.play();

2 Responses to “How To Embed An MP3 In Haxe With FlashDevelop”

Alex Jeffery