Update: Zach Callear has published a complete shoutcast player in flash without memory leaks based on the tricks described in this post.
Update: For those interested in an easy streaming solution, try haxeVideo.
Trying to play music on the Wii, I’ve encountered a problem in the flash Sound object.
While you listen to an mp3 in streaming mode:
), flash will keep in memory the music played.
A Shoutcast/Icecast stream like an Internet radio, is like a never ending mp3, witch take a large amount of memory, and eventually make the player crash. In the Wii this happen after few minutes.
After some googling, I’ve found that the only solution is to reload the stream after some time, freeing the buffer.
On a pc this can be made every 30 minutes, but on the Wii every 2 minutes.
To avoid the pause when flash reload the stream, is possible to preload in the background another Sound object, and then crossfade the two. In this way there is only an imperceptible glitch due to the lack of synchronization of the two streams.
So here how to do a radio player in flash without the memory leak:
- In a Sound object
snd1load the stream
- After some time (2 minutes on the Wii, depending on the bitrate), start loading another Sound object,
- After a “buffer time” (depending on the connection, 10 second should be fine) fade the two Sound
- Free the fist sound
snd1 = null
- Repeat from step 2
Remember that you have to pass a movieclip to the Sound object:
[as] var snd1 = new Sound(_root.sndMc1);
[/as] And when you free the object you have to wait a frame for the garbage collector to free the memory:
[as] snd1 = null;
snd1 = new Sound(_root.sndMc1);
[/as] Update: You can reload the same stream in another Sound object in this way:
[/as] Without the
";"+Math.random() Flash will reuse the same connection
Here the source code of a proof-of-concept player:
Internet radio flash player