Shorts
Seamless mp3-playback in flash
20:52
I have the pleasure of working with flash. And when I say pleasure, I actually mean it. Except for on those special occasions, which are more frequent than I’d like them to be.
My latest task was to create a simple tracker(Wikipedia: Tracker). You’ve all seen one of them; drag the samples on to the timeline and press play on tape to hear your masterpiece. All good and dandy, sounds like a fun little project. My only worry was regarding playing back the samples one after another, as working with sounds in flash isn’t quite as smooth as I’d like it to be.
Luckily, we’ve already (heh) migrated to Flash 9 and AS3(Actionscript 3), so I figured most of the pain I remembered from the days of AS2(Actionscript 2) would be gone by now. But how wrong was I…
Flash is not alone to take the blame though. As it turns out, mp3 encoders always create a short lead in and out when encoding. That’s problem number 1.
Problem number 2 is that the Event.SOUND_COMPLETE
-event flash so conveniently provides you with, isn’t usable in this case, because playing sounds sequentially when the event triggers, results in a short but disastrous gap between sounds, making the event useless (in this case).
Problem number 3 is an interesting one. Let’s say, that I have a 1.8 seconds long sound. When I load that sound into my flash movie, the Sound
-object will say that the duration is 1800 milliseconds. However, when I play it back, the SoundChannel
-object will trigger the soundComplete
-event a bit before the Sound.position
reaches that magical 1800 marker.
It seems, that the good folks at Macromedia Adobe have tried to create a mechanism that works around the automagically inserted lead in and out. The article linked to at the top notes that importing a wav-file into the library will result in gapless mp3-encoded sound, and it seems (just speculating here) that the SoundChannel
-object is trying to mimic that behaviour, but failing to do so, or perhaps it’s just the Sound
-object that fails at accounting for the lead in and out when setting its own length.
Firstly, it does in no way account for the lead in. Secondly, the unconsistency between Sound.length and the point where the soundComplete
-event triggers, makes the situation even worse when trying to programmatically work around the shortcomings of the mp3-format and the Sound API (Application Programming Interface).
Lastly, I’ve found no pattern in the manner that the Sound.length
relates to the point where the soundComplete
-event triggers, so I can’t calculate it. (Disclaimer: my tests have not been thorough, rigorous and scientific, so this is not to be taken as absolute truth — it also most probably related to mp3-encoding and not the Sound API, but I’m mostly guessing.) The “solution” I came up with, was to play back the sound silently and record the position where the soundComplete
-event triggers — certainly not elegant, but it works.
For the overall problem of gapless playback, I used an existing “solution” (scroll down to part b) which is basically an event triggering every millisecond to stop and start sounds at the right moment. Quite resource wasteful and certainly not foolproof, but it solved circumvented the problem at hand.
Shorts
Romka Magazine – a magazine for photography and features international amateurs, students and professionals.
Wonderfl.com – write flash online. Check out Ball Blast.
Meta
Subscribe to feeds
Pages
Latest Posts
- Overland from Finland to London: Epilogue
- Overland from Finland to London: Days 3 and 4
- Overland from Finland to London: Day 2
- Overland from Finland to London: Day 1
- Overland from London to Finland: Day 5
- Overland from London to Finland: Day 4
- Overland from London to Finland: Day 3
- Overland from London to Finland: Day 2
- Overland from London to Finland: The Beginning
- The day the .dev gTLD died
Latest comments
- nikc on Managing scrollTop in your Backbone single-page app: Richard, you’re right, you can store...
- Richard Hunter on Managing scrollTop in your Backbone single-page app: Unfortunately, pop state wont help you...
- nikc on Managing scrollTop in your Backbone single-page app: Richard, Thank you. You’re right, scroll position...
- Richard Hunter on Managing scrollTop in your Backbone single-page app: This is a clever solution- however...
- llaurén on Better tools for procrastination: Yer welcome!
- nikc on Better tools for procrastination: Well, after evaluating both, I find...
- nikc on Better tools for procrastination: Having taken a closer look at...
- nikc on Better tools for procrastination: One Tab looks nice, too. But...
- llaurén on Better tools for procrastination: Nice catch! I use One Tab to...
- nikc on How to turn your smartphone (nearly) useless in one simple step: Wow, I’d love for the EU...