This article is a result of my experiences with streaming MP3 streams with AS3 on a ATOM-based device. After playing a stream for a given period of time the Flash Player kept crashing for no apparent reason. After some investigation I found a memory leak within my streaming application, and after further investigation I was able to narrow it down to the Sound class in AS3 (with good help from this blog post by Madarco). I quote:

“While you listen to an mp3 in streaming mode: snd.loadSound(url, true), flash will keep in memory the music played.”.

Now, when you have a continuous stream this means trouble. The memory used by the stream will not be released until the stream is closed.

Based on the info provided in Madarco’s post I spent some time working on a solution that would satisfy my needs. I ended up with an approach that initiates a second stream once the first has a buffer of a given size. Once the limit is reached I initiate the second stream at zero volume, then use GreenSock’s VolumePlugin tween to cross-fade the two streams. Once this is done, I free the memory reserved by the first stream and swap the references.

A complete working implementation can be found at PasteBin. For the sake of demonstration I’m swapping streams after 500kb, but this is clearly not an ideal limit for real world usage. This can also be tweaked to take the amount of used memory and / or time played instead of just looking at the buffer size when deciding when to swap streams. I still have quite some testing ahead before I feel comfortable using this, but it’s a good start. Hope this can help others faced with the same problem.

Leave a Reply



You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">