• home
  • forum
  • my
  • kt
  • download
  • Playing sounds with Actionscript

    Author: 2007-06-06 15:40:20 From:

    In this tutorial you will learn how to play a sound from the library through actionscript and makes its volume and pan (which speaker it comes out) change. I will be using Flash 8, but Flash MX 2004 will work fine as well.

    You can see an example of what we will be making importing the file

    Now if you don¡¯t have the library open, open it by clicking Windows > Library or Ctrl+L. You should see the sound you just imported. Right click it and click on ¡®Linkage¡­¡¯.

    Opening the linkage window

    The linkage window should now be open. Click the box that says ¡®Export for actionscript¡¯ and enter ¡®mySound¡¯ in the identifier box and click ¡®OK¡¯. Now we can refer to our sound through actionscript.


    Linking the sound

    If you paste this code onto the frame:

    ¡­You will attach the sound from the library into a sound object in actionscript called ¡¯sound¡¯. If you want to refer to this sound you can just use the sound object ¡¯sound¡¯ (woah¡­ i said sound 6 times in 2 sentences :O ).

    Now, if you wanted your sound to play, you could add this underneath the code you just got.

    That is using the start() function to make the sound play. The first parameter is the offset to play the sound in seconds. If you had a 60 second clip and you put 10 in this parameter flash would skip the first 10 seconds of the sound and start playing from there. This parameter is optional, so you don¡¯t need to put it in.
    The second parameter is how many times you want the sound to loop. This parameter is also optional.

    If you did want your sound to loop, you could add this to your code.

    The first line of that is telling flash that everytime the ¡¯sound¡¯ sound object stops to run the code between the curly braces. I have just put the same code as we used before to start the sound. So every time the sound finishes playing it will start again.

    Now for the volume and the panning. We won¡¯t be making any special volume slide or anything. The mouses _y coordinates will determine the volume and the mouses _x coordinates will determine the panning. Here is the code:

    I think that is pretty self explanatory. The first line means ¡®When this frame is entered, so the code will happen every time that frame is run. The next one is setting the volume to the mouses _y coordinates, and the 3rd is setting the pan to the mouses _x coordinates. That is it for this tutorial. If you did want to stop your sound, you can use this code:

    sound.stop();

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      3D (20)
      Math Physics (14)
      3rd Party (5)
      Navigation (60)
      Actionscripting (26)
      Optimization (16)
      Animation (32)
      Projector (9)
      Audio (46)
      Special Effects (112)
      Backend (25)
      Text Effects (65)
      Drawing (18)
      Tips and Techniques (41)
      Dynamic Content (25)
      Tricks (6)
      Games (66)
      Utilities (19)
      Getting Started (71)
      Video (10)
      Interactivity (21)
      Web Design (22)

    New

    Hot