Title: Flash: Controlling Sound
1Flash Controlling Sound
- Presentation by Mindy McAdams
2ActionScript (so far)
- From Lessons 1 through 7
- stop()
- play()
- gotoAndStop("someframelabel")
- gotoAndPlay("otherframelabel")
- The script can be written on a button or on a
frame
3ActionScript for buttons
- If script is on a button
- on (release) stuff here
- Alternatively -- if a button has an instance
name, you can put the script on the Timeline this
way - rewind_btn.onRelease function () stuff here
on (release) stuff here
rewind_btn.onRelease function () stuff here
4ActionScript movie clips
- If you need to make a movie clip stop playing (if
the instance name is clouds_mc) from the main
Timeline - Or if the movie clip needs to tell the main
Timeline to stop playing
clouds_mc.stop()
_parent.stop()
5New, exciting ActionScript
- What is a variable?
- A kind of container for data
- Each variable has a name (static)
- Each variable has a value (dynamic)
- Examples of variables (name, left and value,
right) - n 27
- myColor "orange"
6ActionScript 2.0 variables
- Not in the book
- AS 2.0 would like script to be more formal in
style - Examples of the initial declaration of variables
in AS 2.0 - var n 27
- var myColor "orange"
7ActionScript 2.0 variables
- In fact, AS 2.0 would like things to be EVEN MORE
formal - Examples of the initial declaration of variables
in AS 2.0, including strict data typing - var nNumber 27
- var myColorString "orange"
8Selecting ActionScript version
- To make life easy, when following the book, you
can simply change the Publish Settings to AS 1.0 - But eventually, people will use AS 2.0 for
everything
9ActionScript variables
- We should formally declare our variables
- var n 27
- var myColor "orange"
- You can do that in AS 1.0 or 2.0
- However we need not use strict data typing
(Number, String, etc.)
10ActionScript variables
- How does a variable work?
- var n 27
- n n 1
- if (n 28) stuff here
- Another example
- var myColor "orange"
- if (myColor ! " blue")
- stuff here
11ActionScript operators
- is equivalent to
- ! is NOT equivalent to
- gt is greater than
- lt is less than
- gt is greater than or equal to
- lt is less than or equal to
12Variables in Lesson 8 (Sound)
- The book says
- x new Sound()
- Here the variable is x and we are declaring it
for the first time - So to be more formal and correct
- var x new Sound()
- Only the first time you use x in a movie do not
write var after the first appearance of the
variable
13Variables in Lesson 8 (Sound)
- A variable name can be any word (except the
reserved words see pp. 479 - 480) - Its common to use letters such as x, y, and z
as names of variables - Its also common to use camel case in the name
-- e.g. mySound, theMusic, etc.
14Variables in Lesson 8
- After we have declared a variable (with var), we
can use it to do things -- rather like an
instance name - x.start(0, 0)
- x.stop()
- These are the basics of audio in Flash
- (Well get back to this shortly)
15Bringing sound into the FLA
- The only script difference between the two
methods for the Sound object concerns how you
associate your audio file with your variable - x.attachSound("crossroads")
- or
- x.loadSound("rainforest.mp3", true)
x.attachSound("crossroads")
x.loadSound("rainforest.mp3", true)
16External MP3 files (loadSound)
- To load an external audio file, the file MUST BE
in the MP3 file format - Also, the file must have a sampling rate of one
of these three values (on pp. 216 217) - 11.025 kHz (doesnt sound great)
- 22.05 kHz (usually best choice)
- 44.1 kHz (CD quality big file size)
- Otherwise, you will have distorted sound
17Starting and stopping sound
- To stop a Sound object represented by the
variable name x - x.stop()
- (Exactly like stopping a movie clip)
- To play a Sound object (from the beginning)
- x.start(0, 0)
- Script can be on a button or on a frame
x.stop()
x.start(0, 0)
18Options for starting sound
- This will start the sound from the beginning AND
play it only once - -
- This will start the sound from the beginning and
loop it 100 times - -
- This will start the sound 20 seconds after the
beginning and play it only once - -
x.start(0, 0)
x.start(0, 100)
x.start(20, 0)
19Pausing sound
- Before you pause an audio file, you must
determine how much has already played - Then you will be able to start it (later) from
the same point - This property is called position
var p p Math.floor(x.position/1000)
20Pausing sound (2)
- Say the sound file had already played for 5.2
seconds (5,200 milliseconds) - This script will get the number 5,200 and divide
it by 1,000 - Then it will round down the result to a whole
number (not a decimal) - Result p 5
p Math.floor(x.position/1000)
21Pausing sound (3)
- If you have the value of p, you can use that
value to restart the audio file at the correct
point after it has been paused - P
- x
- If the value of p were 5, then the audio would
immediately start playing from the point 5
seconds into the file
p Math.floor(x.position/1000) x.start(p, 0)
22Adjusting the volume
- If all you need to do is mute the audio (while it
continues to play) - -
- If you then need to return the audio to the
maximum volume - -
- The volume range is exactly 0 to 100
x.setVolume(0)
x.setVolume(100)
23ActionScript basics
- From Lessons 1 through 7
- stop()
- play()
- gotoAndStop("someframelabel")
- gotoAndPlay("otherframelabel")
- You will use these in almost every Flash movie
24ActionScript for audio (basics)
- From Lesson 8 (x can be any name)
- var x new Sound()
- x.attachSound()
- x.loadSound()
- x.start(0, 0)
- x.stop()
- You will use these to control sound in any Flash
movie that requires it
25ActionScript/audio (advanced)
- From Lesson 8 (x can be any name)
- var x new Sound()
- var p 0
- p Math.floor(x.position/1000)
- x.start(p, 0)
- x.setVolume(0)
- x.setVolume(100)
- You will use these to control sound in
conjunction with buttons in the Flash movie
26Flash Controlling Sound
- The End
- Presentation by Mindy McAdams
- gt Flash Journalism Lesson 8