Using SharedObject to Mute/Unmute Sound according to the User’s Choice

In this tutorial, we’ll demonstrate the usage of SharedObject a.k.a Flash Cookies. We’ll use it to remember whether the user has turned ON or OFF the sound and when he returns to the page the sound will be played or not.

View DemoDownload Source

1. Create a new Flash file (AS3) and save it as sharedobject.fla.

2. Rename “layer 1” to “Button”. On this layer create the mute/unmute button which is a movie clip with 2 keyframes, the first for the mute and the 2nd for the unmute. Add stop() action on each frame.

3. Back to Scene 1, create an “actions” layer. With the first frame selected open the actions panel.

(For more details about SharedObject check the documentation)

First we call the getLocal() method to create an instance of a SharedObject and retrieve a reference of the shared object which name is “riacodes_sound”. Next we retrieve the ismute info from the SharedObject by accessing the info through the data property.

var mySO:SharedObject = SharedObject.getLocal("riacodes_sound");
var isMute : Boolean = mySO.data.ismute != null ? mySO.data.ismute : false;

4. Next we add the script to load the sound and play it and create the toggleMute function.

var song:Sound = new Sound();
var channel:SoundChannel = new SoundChannel();
var songPosition : Number = 0;

mute_mc.addEventListener(MouseEvent.CLICK,toggleMute;
mute_mc.buttonMode = true;

song.load(new URLRequest("sound.mp3"));
if(!isMute)
	channel = song.play();
else 
	mute_mc.gotoAndStop(2);



function toggleMute(e:MouseEvent):void{	
	if(!isMute){
		songPosition = channel.position;
	        channel.stop();
		isMute = true;
		mute_mc.gotoAndStop(2);
	}
	else { 
		channel = song.play(songPosition);
		isMute = false;
		mute_mc.gotoAndStop(1);
	}

}

5. Last thing that we need to do, is to store the ismute info data each time the user mute or unmute the sound. To do so, at the end of the toggleMute function, we add ismute data to the SharedObject by setting its value to the isMute Boolean variable and call the flush() method to write the data inside the end user’s machine.

function toggleMute(e:MouseEvent):void{	
	if(!isMute){
		songPosition = channel.position;
	       channel.stop();
		isMute = true;
		mute_mc.gotoAndStop(2);
	}
	else { 
		channel = song.play(songPosition);
		isMute = false;
		mute_mc.gotoAndStop(1);
	}
	
	mySO.data.ismute = isMute;
	mySO.flush ();
}

6. Here’s the final code, test your movie and try playing with the mute button and refresh your page to see the code in action.

var mySO:SharedObject = SharedObject.getLocal("riacodes_sound");
var isMute : Boolean = mySO.data.ismute != null ? mySO.data.ismute : false;

var song:Sound = new Sound();
var channel:SoundChannel = new SoundChannel();
var songPosition : Number = 0;

mute_mc.addEventListener(MouseEvent.CLICK,toggleMute);
mute_mc.buttonMode = true;

song.load(new URLRequest("sound.mp3"));
if(!isMute)
	channel = song.play();
else 
	mute_mc.gotoAndStop(2);
	

function toggleMute(e:MouseEvent):void{
	if(!isMute){
		songPosition = channel.position;
	    channel.stop();
		isMute = true;
		mute_mc.gotoAndStop(2);
	}
	else { 
		channel = song.play(songPosition);
		isMute = false;
		mute_mc.gotoAndStop(1);
	}
	
	mySO.data.ismute = isMute;
	mySO.flush ();
}

6 thoughts on “Using SharedObject to Mute/Unmute Sound according to the User’s Choice

Leave a Reply

Your email address will not be published. Required fields are marked *