Download presentation
1
Thomas Lövgren, Flash developer
Introduction to Flash ActionScript 3.0 Sound Tid: ca 1,5 timme – 10 talet exempel Thomas Lövgren, Flash developer
2
Introduction to Sound Lecture Outline
In this lecture we’ll discuss and practice the following topics: AS3 Sound Architecture Loading and playing External and Internal sound Sound related classes Coding a basic Sound Player More sound functions & features Microphone Sound Visualization
3
Sound AS3 Sound Architecture In AS3 there are a lot of useful classes and methods for different kinds of sound functionality Sound class: Is used to load, play and manage basic sound properties SoundChannel class: Is used to create a separate channel for each new sound played (“channel” does not refer to left or right channel of stereo) SoundMixer class: Creates a central mixer object through which all sound channels are mixed SoundTransform class: Is used to control the volume, and panning between left and right stereo channels of a source Microphone class: Let’s us control the gain, sampling rate and other properties of a Microphone
4
Internal and External Sound
In Flash there are basically two different ways of working with sound Internal (sound in library) and External Sound (loading a sound) It’s recommended that we use external sounds - by loading a sound into the application, we can keep the file size down Examples of how to use the different types: External sound: Load a sound file (MP3) into the application. This could be for example: background music, mp3 player etc. Internal sound: Attach sound from Library (linkage). Small sounds (file size), for example sound effects for a game, button click-sounds etc.
5
Sound Sound class (1/2) The Sound class is used to load, play and manage basic sound functionalities Some useful properties, methods & events for the Sound class: Sound(), load(), play(), complete, bytesLoaded, bytesTotal, length etc //create a new sound object var my_sound:Sound = new Sound();
6
Sound class (2/2) (Load & play: External Sound)
Here is an example of how to load and play an external MP3 sound, and play it by using the Sound class var my_sound:Sound = new Sound(); //create a new sound object my_sound.load(new URLRequest("David Gray - Sail Away.mp3")); //load my_sound.play(); //play sound Note! It’s recommended that we also uses Events/functions to check if the loading is complete
7
Sound SoundChannel The SoundChannel class, can be used to create a separate channel for each new sound played, stop a sound and/or get sound-peaks An application can have multiple sound channels that are mixed together Some useful properties, methods & events for this class: Stop(), soundComplete, leftPeak, rightPeak, position etc var my_sound:Sound = new Sound(); //create sound object var my_channel:SoundChannel = new SoundChannel(); //create channel my_sound.load(new URLRequest("David Gray - Sail Away.mp3")); //load my_channel = my_sound.play(); //play sound through channel
8
Internal Sound (Sound in library/attach a sound)
For playing a sound from library – first we need to import the sound to Flash, then set up the Linkage property in library 1. Right-click on the sound in library 2. Select Linkage (Properties in CS4) 3. Check the box “Export for ActionScript” //create a new "library" sound & channel object var library_sound:explode_sound = new explode_sound(); //sound object var explode_channel:SoundChannel; //create channel object explode_channel = library_sound.play(); //play sound through channel
9
Sound SoundTransform class The SoundTransform class, allows us to adjust the sound volume, and/or panning between left and right channel of a source Some of the properties and methods for this class: SoundTransform(), pan, volume etc //function that adjust the volume by dragging a slider function adjustVolume(event:Event):void{ var vol:Number = volume_mc.slider_mc.x / 100; var st:SoundTransform = new SoundTransform(vol); }
10
Sound SoundMixer class The SoundMixer class creates a central mixer, changes to the mixer will affect all sounds Some of the properties and methods for this class: stopAll(), computeSpectrum(), bufferTime etc //function that stops all sounds function stopAllSound(event:MouseEvent):void{ SoundMixer.stopAll(); }
11
Sound Stop, pause & play sound There are a couple of more interesting sound features, for example if we want to stop, pause and play a sound we can write like: //declare variable, get the sound position var pausePos:Number = sound_channel.position; sound_channel.stop(); ...and at a later point we can start the sound at that position by writing: sound_channel = my_sound.play(pausePos);
12
Sound Sound Complete If we want a sound to repeat when finished, we can add a listener to the Sound/channel object with the event SOUND_COMPLETE //add listener to the sound through the channel object, call method my_channel.addEventListener(Event.SOUND_COMPLETE, soundComplete); //handler/function for repeating the sound function soundComplete(event:Event):void{ my_channel = my_sound.play(); //play sound again }
13
Sound Microphone The Microphone class allows us to connect a microphone to Flash By using the activityLevel method, it’s possible to for example animate the “activity” directly from the microphone var my_mic:Microphone = Microphone.getMicrophone(); Security.showSettings(SecurityPanel.MICROPHONE); my_mic.setLoopBack(true); my_mic.setUseEchoSuppression(true); stage.addEventListener(Event.ENTER_FRAME, onLoop); function onLoop(event:Event){ line_mc.width = my_mic.activityLevel * 5; activity_dyn.text = "Activity: " + String(my_mic.activityLevel) + "%"; width_dyn.text = "Line_mc Width: " + String(line_mc.width) + "px"; trace(my_mic.activityLevel); }
14
Sound Sound visualization By using the SoundMixer class and the computeSpectrum method, we can create interesting and fancy animations synchronized with the sound source
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.