Sound Action in ActionScript 3


Sound Controls

Usually creating a button in Adobe Flash that allows a user to turn on and off sounds that will at the same time display feedback whether sounds are 'unmuted' or 'muted' is fairly easy. It works by basically using a simple graphical button with transparency of a speaker, without any sound waves coming out of it. Over the top of that, we add two additional movieclip images with transparency for off (has a line over the speaker or says 'OFF' or does nothing to indicate that there is no sound playing), and on (sound waves are protruding from the speaker or says 'ON' to indicate there is sound playing).

Programming this is then simple, as it simply makes these movieclips layered over the top visible or not visible without affecting the actual button. Controlling the sound on/off is done with ActionScript and therefore the track needs to be exported for ActionScript. This is mySong below. soundon and soundoff without camelCase are the instance names of the overlaying movieclips.

Below code was provided:

var soundOn:Boolean = true;

var s:Sound=new mySong();
var song:SoundChannel=s.play();
setVolume();
// Add only if you want it to loop
song.addEventListener(Event.SOUND_COMPLETE,soundCompleteHandler);

function setVolume():void {
	var vol:Number;
	if (soundOn) {
		vol = 0.5;
	} else {
		vol = 0.0;
	}
	var st:SoundTransform = new SoundTransform(vol);
	if (song != null) {
		song.soundTransform = st;
	}
}
function soundCompleteHandler(event:Event):void {
	song.stop();
	song=s.play();
	setVolume();
	song.addEventListener(Event.SOUND_COMPLETE,soundCompleteHandler);
}

var marker:Number = 0;
var soundPlaying = true;
soundoff.visible = false;

function controlSound(event:MouseEvent):void {
	if (soundPlaying) {
		soundPlaying = false;
		marker = song.position;
		song.stop();
		soundon.visible = false;
		soundoff.visible = true;
	} else {
		soundPlaying = true;
		song=s.play(marker);
		soundon.visible = true;
		soundoff.visible = false;
	}
}
soundonoff.addEventListener(MouseEvent.MOUSE_DOWN, controlSound);

However for the project which I was working on, two separate graphics for mute and unmute were used. This simplified the ActionScript somewhat, but perhaps presented some new problems since I did not adapt the script completely, however rather than focusing on muting a single track, this ActionScript mutes ALL sounds that are playing. It is functional, at least.

Usually when we want to turn off all sounds, we use SoundMiver.stopall(); which will stop all the sounds in the project. However, in my experience this doesn't allow you to turn the sound back on again. Thus in this instance, this code works by using SoundMixer.soundTransform = new SoundTransform(0);, I discovered in my searching. Setting the SoundTransform() value as 0 means the sound is muted, and setting the value to 1 will play sounds at full volume again.

mutebutton refers to the movieclip that shows when the sound is ON, that is, you want to mute the sound, and volbutton refers to the movieclip that shows when the sound is OFF, that is when you want to turn the volume back on.

this.mutebutton.visible = true;
this.volbutton.visible = false;

this.mutebutton.addEventListener(MouseEvent.CLICK, fl_ClickToStopAllSounds);
this.volbutton.addEventListener(MouseEvent.CLICK, fl_ClickToStopAllSounds_2);

//CLICK TO MUTE
function fl_ClickToStopAllSounds(event:MouseEvent):void
{
	SoundMixer.soundTransform = new SoundTransform(0); //MUTES
	this.mutebutton.visible = false; //HIDES UNMUTED BUTTON
	this.volbutton.visible = true; //SHOWS MUTED BUTTON
}

//CLICK TO UNMUTE
function fl_ClickToStopAllSounds_2(event:MouseEvent):void
{
	SoundMixer.soundTransform = new SoundTransform(1); //UNMUTES
	this.mutebutton.visible = true; //SHOWS UNMUTED BUTTON
	this.volbutton.visible = false; //HIDES MUTED BUTTON
}

This worked a treat. However my ActionScript to play sounds when skipping to a different frame in the swf did not always work as well, particularly when loading in child swfs. However it works within the swf just fine. In the below frame refers to a non-sequential frame in the swf, and mySound is the AS Linkage name given to the sound file.

skipbutton.addEventListener(MouseEvent.CLICK, skipanimation);
function skipanimation(e:MouseEvent):void
{
MovieClip(root).gotoAndStop("frame");
var s:Sound=new mySound();
var song:SoundChannel=s.play();
}

For some reason I could not get the sound to be anything other than full volume, but there is likely something I missed.

Sound is really a difficult thing to play with in Adobe Flash and we underestimated how hard it could really be. It's time consuming, but it really adds to the project and is a necessary evil in the end. Oh woe.