Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sound Flash ActionScript 3.0 Introduction to Thomas Lövgren, Flash developer

Similar presentations


Presentation on theme: "Sound Flash ActionScript 3.0 Introduction to Thomas Lövgren, Flash developer"— Presentation transcript:

1 Sound Flash ActionScript 3.0 Introduction to 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 AS3 Sound Architecture Loading and playing External and Internal sound Loading and playing External and Internal sound Sound related classes Sound related classes Coding a basic Sound Player Coding a basic Sound Player More sound functions & features More sound functions & features Microphone Microphone Sound Visualization Sound Visualization Sound

3 AS3 Sound Architecture In AS3 there are a lot of useful classes and methods for different kinds of sound functionality 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 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) 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 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 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 Microphone class: Let’s us control the gain, sampling rate and other properties of a Microphone Sound

4 Internal and External Sound In Flash there are basically two different ways of working with 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 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: 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. 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. Internal sound: Attach sound from Library (linkage). Small sounds (file size), for example sound effects for a game, button click-sounds etc. Sound

5 Sound class (1/2) The Sound class is used to load, play and manage basic sound functionalities The Sound class is used to load, play and manage basic sound functionalities Some useful properties, methods & events for the Sound class: Some useful properties, methods & events for the Sound class: Sound(), load(), play(), complete, bytesLoaded, bytesTotal, length etc Sound(), load(), play(), complete, bytesLoaded, bytesTotal, length etc //create a new sound object var my_sound:Sound = new Sound(); Sound

6 Sound class (2/2) (Load & play: External Sound) 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 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 loading is complete Sound

7 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 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 An application can have multiple sound channels that are mixed together Some useful properties, methods & events for this class: Some useful properties, methods & events for this class: Stop(), soundComplete, leftPeak, rightPeak, position etc 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 Sound

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 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 1. Right-click on the sound in library 2. Select Linkage (Properties in CS4) 2. Select Linkage (Properties in CS4) 3. Check the box “Export for ActionScript” 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 Sound

9 SoundTransform class The SoundTransform class, allows us to adjust the sound volume, and/or panning between left and right channel of a source 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: Some of the properties and methods for this class: SoundTransform(), pan, volume etc 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); } Sound

10 SoundMixer class The SoundMixer class creates a central mixer, changes to the mixer will affect all sounds The SoundMixer class creates a central mixer, changes to the mixer will affect all sounds Some of the properties and methods for this class: Some of the properties and methods for this class: stopAll(), computeSpectrum(), bufferTime etc stopAll(), computeSpectrum(), bufferTime etc //function that stops all sounds function stopAllSound(event:MouseEvent):void{ SoundMixer.stopAll();} Sound

11 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: 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); Sound

12 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 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 } Sound

13 Microphone The Microphone class allows us to connect a microphone to Flash 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 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);} Sound

14 Sound visualization By using the SoundMixer class and the computeSpectrum method, we can create interesting and fancy animations synchronized with the sound source By using the SoundMixer class and the computeSpectrum method, we can create interesting and fancy animations synchronized with the sound source Sound


Download ppt "Sound Flash ActionScript 3.0 Introduction to Thomas Lövgren, Flash developer"

Similar presentations


Ads by Google