EXswap

Playing OGG sounds in Flash/Flex

Although there is a feature request on Adobe Bugs, it didn’t get much attention from Adobe: FP-3037. So, in the mean time…

Playing OGG sounds in Flash has been possible for a couple of months now, with the new FP10 Sound API and AS3 OGG Vorbis library built using Adobe Alchemy and available on Adobe Labs: http://labs.adobe.com/wiki/index.php/Alchemy:Libraries.

The strategy is to:

  1. load the sound with URLLoader as bytes (it can also be embeded, but it wouldn’t make much sense);
  2. use AS3 OGG Vorbis library to parse the data and create the OGGSound (a class created specifically for this demo that wraps an OGG sound byte array); the decoder is OGGDecoder;
  3. simulate the sound api by extending Sound class and make use of the new SampleDataEvent.SAMPLE_DATA to read from the samples and output to the audio card.

I’m removing the last 2 samples in order for the sound to loop smoothly.

Give a test to the small demo project below and take a look at the code here.

Get Adobe Flash player

The fact that you need to do:

1
2
3
var sound:Sound = new Sound();
sound.addEventListener(SampleDataEvent.SAMPLE_DATA, onSampleData);
sound.play();

…, in order for this to work seems a bit hacky, since in fact you’re creating a dummy empty sound that you lively populate. Maybe there could be something more intuitive…

Bookmark and Share

Post to Twitter

Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!

EXswap