FlashDevelop/EmbeddingSounds

From Chumby Wiki
Jump to: navigation, search

In order to embed sounds with flash develop, you will need to create a separate movie using swfmill.

Obtain a suitable source sound

swfmill is pretty fussy about the source files it accepts. The main gripe appears to be with the sample rate, which should be 44100Hz. The following should help you get an MP3 file in the correct format.

  1. Find the sound sample in .WAV format. A good source is this collection of public domain sound samples.
  2. Download lame. Binaries for windows can be found via google. Once downloaded, use the following to generate a suitable MP3 file from your WAV file
lame -b 64 --resample 44 BEEPPURE.WAV
This will create the file BEEPPURE.WAV.mp3 in the same directory.

Creating an SWF file from your MP3

Once you have a suitable sound source, you need to use swfmill to create an swf file that contains the sound file in order to import it as a library into your Flashdevelop project. The swfmill program is guided by an XML file. Use a text editor to create an XML file called sounds.xml similar to this:
<?xml version="1.0" encoding="iso-8859-1" ?>
<movie width="320" height="240" framerate="12">
  <background color="#ffffff"/>
<frame>
  <library>
    <sound id="beep2ID" import="BEEPPURE.WAV.mp3"/>
  </library>
</frame>
</movie>
where the id beep2ID above will be how you refer to the sound in your ActionScript code and the string after the “import” above is the name of your source MP3 file.

Because FlashDevelop uses swfmill, you have probably already have a copy. I found it in C:\Program Files\FlashDevelop\FirstRun\Tools\swfmill\swfmill.exe. From there run it from a DOS command line like this:

    swfmill simple sounds.xml sounds.swf

which should produce the sounds.swf file that you will use in your FlashDevelop project. If you receive an error like this WARNING: this file is not a valid MP3 then your source file likely has an incorrect sample rate.

Referencing the SWF file from your Flashdevelop Project

Move the resulting swf file into the folder with your FlashDevelop source. Then within FlashDevelop, find this file name in the panel on the right side. Note that the filename is black. Next right click on the file name and from the pop-up menu, select Add to Library. Then the filename will turn blue.

In your ActionScript code, you would put something like the following:

static var _beep2: Sound = new Sound();

static function Main(mainMc:MovieClip){
    _beep2.attachSound("beep2ID");
    _beep2.setVolume(10);  // Because otherwise its too loud
    ...
    // Upon push of a button...
    beep2.start();
}

Compile and run. The sounds did not work on the Flash player integrated with the FlashDevelop environment, but they worked fine on the Chumby.


This article is adapted from the following forum post, so credit to anyone who contributed there.