Tag Archive for 'localConnection'

King of the LocalConnection

The Challenge:
Make a small mp3 player that's no more than a play button. Scatter many of these miniPlayers on a single web page. When one is clicked to play, the currently playing one needs to stop.

The Problem:
Multiple movies cannot be connected to a localConnection at one time. LocalConnection only allows for a one-to-one connection.

The Solution:
King of the Hill! Since only one swf can play at a time, then that swf becomes king of the hill. If another swf pushes him off, then the new swf takes over the localConnection.

Here's how it works:
First, notice that we're using a flashvar to pass in the reference to the mp3. In the downloadable code, there will be an index.html file which sets the flashvar using swfObject.

When the user clicks the playButton, the miniPlayer performs a localConnection.send() function and passes along it's uniqueId. The "King" of the connection will receive the call and if the uniqueId is not its own, he'll step down off the hill and close it's connection. The new King then calls localConnection.connect() and claims the localConnection throne. That's really the whole story.

Below is some sample code. The full code is here:

Comments