Using imported sounds in Flash CS3 with Actionscript 3

Posted 30th July 08 by Matt Cain

So you've imported a sound into your library within Flash CS3, but now you want to play it at the time of your choosing using Actionscript. How do you do that? It's not as obvious as you might think:

1. Right click your sound in the library and click Linkage or Properties.

2. Check the "export for actionscript" checkbox.

3. Give it a suitable class name.

4. You then need to create a SoundChannel object to play your sounds.

import flash.media.SoundChannel;
var sc:SoundChannel=new SoundChannel();
sc=new MySound().play();

Of course "MySound" is going to need to be replaced by the class name you gave your sound.

Now suppose you want to change the volume of your sound? No problem.

import flash.media.SoundTransform;
sc.soundTransform=new SoundTransform(0.5);

This'll play the sound at 50% volume. Remember that you can't just change the properties of the SoundChannel's soundTransform property, you have to make a new SoundTransform object and replace the old one. Also remember that the volume must be between 0 and 1.

Time for some practical work. A music player which demonstrates pausing and stopping sound. It also demonstrates looping a sound.

package {
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.media.SoundChannel;
	import flash.media.SoundTransform;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import fl.events.SliderEvent;
	public class MusicPlayer extends MovieClip {
		//A property to save te position of the sound when it gets paused
		var playPosition:Number=0;
		var soundChannel:SoundChannel=new SoundChannel();
		//A property to keep track of whether the sound is currently playing
		var isPlaying:Boolean=false;
		function MusicPlayer():void {
			//Add the event listeners that listen for button clicks
			//and the slider being moved.
			pButton.addEventListener(MouseEvent.CLICK, playOrPause);
			sButton.addEventListener(MouseEvent.CLICK, stopSound);
			slider.addEventListener(SliderEvent.CHANGE, changeVolume);
		}
		private function playOrPause(m:MouseEvent):void {
			//If the sound is not currently playing, play it.
			if (!isPlaying) {
				soundChannel=new Sound().play(playPosition);
				soundChannel.soundTransform=new SoundTransform(slider.value);
				soundChannel.addEventListener(Event.SOUND_COMPLETE, loopSound);
				isPlaying=true;
			}
			//If the sound is playing, pause it.
			else if (isPlaying) {
				//Save the current position so you can resume
				//the sound from where it was stopped.
				playPosition=soundChannel.position;
				soundChannel.removeEventListener(Event.SOUND_COMPLETE, loopSound);
				soundChannel.stop();
				isPlaying=false;
			}
		}
		private function stopSound(m:MouseEvent):void {
			if (isPlaying) {
				//Make sure the sound will start back at the beginning
				//if it is played again.
				playPosition=0;
				soundChannel.removeEventListener(Event.SOUND_COMPLETE, loopSound);
				soundChannel.stop();
				isPlaying=false;
			}
		}
		private function changeVolume(s:SliderEvent):void {
			//This sets the volume to be the sliders current value.
			//I've changed the sliders parameters so this'll work
			//check in the FLA.
			soundChannel.soundTransform=new SoundTransform(slider.value);
		}
		private function loopSound(e:Event):void {
			//Removes the old listener
			soundChannel.removeEventListener(Event.SOUND_COMPLETE, loopSound);
			soundChannel=new Sound().play();
			//The new sound will have full volume, so you need to set
			//the correct volume again.
			soundChannel.soundTransform=new SoundTransform(slider.value);
			//Adds the new listener
			soundChannel.addEventListener(Event.SOUND_COMPLETE, loopSound);
		}
	}
}

Javascript and Flash Player 9 required to view this Flash Object

You can download the source files here :)

Thanks for reading, hope this is of some help. Feedback always welcome.

Check out our other Articles

IX Web Hosting Review

IX Web Hosting Review

Posted: 19th August 08

A brief review of the woeful 2 months that I spent with IX Web Hosting! READ MORE

Tinting with Actionscript 3

Tinting with Actionscript 3

Posted: 29th July 08

Tints are cool. Knowing how to use them dynamically via Actionscript 3 is definitely handy. This article shows you how. READ MORE

Or check out some Flash!

The Gaming Quiz

The Gaming Quiz

Posted: 20th September 08

A quiz about Video Games and the Gaming industry. Take the quiz and see how much you really know about gaming. PLAY THIS GAME

Earth Defender

Earth Defender

Posted: 2nd September 08

Defend the world from an alien invasion and generally shoot things in this arcade style space shooter. PLAY THIS GAME

MochiAds