Memory leaks playing Shoutcast/Icecast streams in Flash

Posted in Flash at 1:29 pm by Madarco

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: snd.loadSound(url, true), 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:

  1. In a Sound object snd1 load the stream
  2. After some time (2 minutes on the Wii, depending on the bitrate), start loading another Sound object, snd2
  3. After a "buffer time" (depending on the connection, 10 second should be fine) fade the two Sound
  4. Free the fist sound snd1 = null
  5. Repeat from step 2

Remember that you have to pass a movieclip to the Sound object:

Actionscript:
  1. var snd1 = new Sound(_root.sndMc1);

And when you free the object you have to wait a frame for the garbage collector to free the memory:

Actionscript:
  1. snd1 = null;
  2. //next frame..
  3. snd1 = new Sound(_root.sndMc1);

Update: You can reload the same stream in another Sound object in this way:

Actionscript:
  1. snd2.loadSound("http://server:8000/;"+Math.random());

Without the ";"+Math.random() Flash will reuse the same connection

Here the source code of a proof-of-concept player:
Internet radio flash player

Listening to music on the Wii

Posted in Flash, Wii at 12:57 pm by Madarco

Update: Now it's needed only a shoutcast server, I'm working on removing the web server too

Wii I own a Wii!
And I'm playing more with the opera browser+flash than with Zelda...
My first experiment is to stream music from the pc to the Wii.

So here a quick how to:

Note: I've encountered some problems because of a weird bug in the flash Sound object, witch I'll explain in the next post. Update: here the post.
There are still some imperceptible "glitches" every two minutes, but the overall quality is good.

Notice that the player hasn't any interface yet and its required to run three(!) servers. In the next days I'll try to improve those things.

Requirements

You need:

It is possible to swap every item in the list with a Mac/Linux alternative, I'll leave it to google.

The server(s)

Download and install the SHOUTcast server.

In sc_serv.ini change the password and make it private:

CODE:
  1. Password=changeme
  2. PublicServer=never

Now start the server.

This has to run on the same machine of the webserver due to the security restrictions of the flash player.
We need two servers because flash refuse to open another connection to the same server. (And we need to do that to avoid memory leaks)

Stream

Download and install the Shoutcast winamp plugin.

Start winamp.

Select Options->Preferences->DSP/Effect->Nullsoft SHOUTcast Source DSP
and put the new password and the server port (8000 by default)

Shoutcat plugin settings
Now the music you listen will be streamed to the shoutcast servers.

The player

Now, get the swf player:
Shoutcast stream flash player

It hasn't any interface yet, I'll do that in the next days.

Put the swf in your web server and pass your ip as a flashVar in wiinamp.html:

HTML:
  1. <param name="movie" value="stream.swf" />
  2. <param NAME=FlashVars VALUE="stream1=http://192.168.1.33:8000/" />
  3. <embed src="stream.swf" FlashVars="stream1=http://192.168.1.33:8000/" />
  4. </object>

8000 and 8002 are the ports of 8000 is the port of the stream servers.

Listen

In the Wii Internet Channel open http://*yourip*/wiinamp.html and relax :)

To Do

  • An interface for the player
  • try Icecast to get rid of the second server and the webserver (for now it made flash unstable)

I’m graduated!

Posted in General at 1:57 pm by Madarco

I've received a degree in Computer Science with a thesis on connections efficiency optimization in the SEDA architecture.
Now I'm officially unemployed!

Landcraft

Posted in Flash, General at 10:21 pm by Madarco

This is a STUNNING landscape generator in Flash.
I've thought about doing something like this so many times, but I didn't know from where to start I wonder how many games can be made with it.
Visit: Landcraft - landscape generator in Flash

Read the rest of this entry »

Flash constant frame rate

Posted in Flash at 8:53 pm by Madarco

A common problem in flash games, especially if there are some heavy graphics effects, is to handle the frame rate correctly. Even if it is possible to set a desired frame rate, the flash player will only try to keep it, but the actual frame rate may vary.
I've googled for some way to solve this [...]

Read the rest of this entry »

Suggested Firefox add-on

Posted in General at 2:32 pm by Madarco

I've found this little Firefox add-on: Tamper Data:
Use Tamper Data to view and modify HTTP/HTTPS headers and post parameters.
It is useful to see the headers sent, the response time and to alter form data, but the best feature is the connections graph.
Worth a try.

Read the rest of this entry »

Scroll browser window inside Flash

Posted in Flash at 2:13 pm by Madarco

Updated: fixed some typos
I've found that resizing a flash movie to fit his content has a little drawback: the mouse wheel doesn't scroll the browser window when the movie has focus, and in my case the movie has width: 100%.
Luckily I've found a nice javascript to scroll the page.
Add this javascript to the page:
JAVASCRIPT:
PLAIN TEXT

function [...]

Read the rest of this entry »

Resize Flash at runtime

Posted in Flash at 9:14 pm by Madarco

If your content is bigger than the average screen size, using a flash scrollbar for the entire document is really slow and not user-friendly, so its better to enlarge flash to fit the content and scroll the entire page with the browser's scroll bar.
Put the flash object in a div:
HTML:
PLAIN TEXT

...

<SCRIPT LANGUAGE="JavaScript">

<!--

function newSize(width,height) {

    [...]

Read the rest of this entry »

Flash max connections

Posted in Flash at 2:05 am by Madarco

Recently I'm making a Flash photo gallery for my dad and I'm trying to optimize the loading of many pictures at once.
The question is: how many concurrent download can Flash make?
Google seems to have no answer, so I decided to make a small test to find it out by myself.
So I've made an swf that [...]

Read the rest of this entry »

Feed problem

Posted in General at 8:38 pm by Madarco

It seems that various aggregators have had problems with my last isp change, Feedburner now seems to work, bloglines, are you still there?

Read the rest of this entry »