AS3: Loading sound from bytearray

Posted on July 8, 2009

4


When fiddling with using zip in Flex/As3, I discovered that the Sound class doesn’t have a .loadBytes method (corresponding to the Loader class .loadBytes method). I was able to get the byteArray data out of the zip, but no way to feed a Sound object with it! Grr..!

Luckily, there are lots of gifted bravehearts and generous brightheads around in the blogosphere!

Thanks to article by Chris at flexiblefactory.co.uk about reading sound from a FileReference, I could make it work! Here is how his elegant solution works – well, at least “kind of how”…:

  • Reading the mp3 bytearray code from the file
  • Building a SWF from the ground up, including the messy header stuff, and embedding the mp3 data into it
  • Loading the SWF into a Loader object, and typecast it from there as a SoundClass to a Sound object
  • Play! :-)

Thank you, Chris!

About these ads
Tagged: , ,
Posted in: Actionscript 3, Flex