tutorial dasar sound flash AS3
Sabtu, 11 Agustus 2012
0
komentar
Exercise files accompany this article. Please make sure to save them both inside the same folder.
Let's begin.
You can use the AS3 Sound class to control the sound in your Flash movie using code. The AS3 Sound class can be used to:
- load an external sound file into a Flash movie
- start the playback of a loaded sound file
NOTE: You can NOT use the Sound class to stop  playing sound. It does not have a method for that. If you want to stop a  sound's playback, you'll have to use a different class - the AS3 SoundChannel class - which we will talk about later.
Open the Sound.fla file. You'll see that there are two buttons on the stage - play_btn and stop_btn. But let's ignore those buttons for now. First, let's write some code that will load and play the sound right away. Select frame 1 of the Actions layer, then go to the Actions Panel and type in the following lines:
var mySound:Sound = new Sound();
var myURL:URLRequest = new URLRequest("CheerfulSong.mp3");The second line (var myURL:URLRequest = new URLRequest("CheerfulSong.mp3");) creates a URLRequest object. This is used to specify the path to the external file that we want to load.
So now what we've created those two objects, we can now load the external sound file. Use the load() method of the Sound class and pass to it the URLRequest object:
Sound.load(URLRequest);
So in our example, we would write:
mySound.load(myURL);
This instructs our mySound object to load the file specified in myURL (which is CheerfulSong.mp3).
var mySound:Sound = new Sound();
var myURL:URLRequest = new URLRequest("CheerfulSong.mp3");
mySound.load(myURL);NOTE: If you want to load more than one sound file, then create another Sound object for each succeeding sound file that you'd like to load.
If you test the movie at this point, you won't hear the sound just yet. That's because we just told Flash to load the sound. We haven't given the instruction to start playing the sound yet. To make the sound start playing, we use the play() method of the Sound class:
var mySound:Sound = new Sound();
var myURL:URLRequest = new URLRequest("CheerfulSong.mp3");
mySound.load(myURL);
mySound.play();The play() method of the Sound class has 2 optional parameters:
- startTime - this lets you specify the point (in milliseconds) where the sound playback should start
- loops - this lets you specify how many times the sound should repeat playback
mySound.play(25000, 2);
The first value refers to the startTime parameter. Here, we've specified a value of 25000 milliseconds (or 25 seconds). This means that when the sound begins playing, it will start playing at the sound's 25-second mark, instead of starting at the beginning. So it will skip the first 24 seconds of the song when the playback starts.
The second value refers to the loops parameter. We've specified a value of 2. This means that the sound will play 2 times. When it plays the first time and then reaches the end, it will loop back to the startTime and play one more time.
Test the movie in order to verify, and then change the play statement back to mySound.play(); (without any parameters), and let's continue.
If we want the sound to play only after the play button (named play_btn) has been clicked, then we'll have to create a mouse click event listener for the play button, and we'll transfer the play sound statement inside that event listener:
var mySound:Sound = new Sound();
var myURL:URLRequest = new URLRequest("CheerfulSong.mp3");
mySound.load(myURL);
// Make sure that you remove the play sound statement
// that was here, and transfer it into the mouse click
// event listener function
play_btn.addEventListener(MouseEvent.CLICK, playSound);
function playSound(e:MouseEvent):void
{
     mySound.play();
}So now that we can play the sound, how do we stop it?
You can NOT stop the sound using the Sound class (e.g. Sound.stop(); will not work). Another class is used for that - the SoundChannel class - which has a stop() method that will let you stop a sound's playback.
Let's go ahead and create an instance of the SoundChannel class. I'm going to name it mySoundChannel.
var mySound:Sound = new Sound();
var myURL:URLRequest = new URLRequest("CheerfulSong.mp3");
var mySoundChannel:SoundChannel = new SoundChannel();
mySound.load(myURL);Think of the SoundChannel as the object that houses or contains your sound. In order to place a sound inside a SoundChannel, you assign the play sound statement to a SoundChannel like so:
SoundChannel = Sound.play();
So in our example, we would write:
mySoundChannel = mySound.play();
This assigns mySound.play() to mySoundChannel, so that the sound is going to play inside mySoundChannel. Let's go ahead and assign mySound.play() to mySoundChannel in our code:
var mySound:Sound = new Sound();
var myURL:URLRequest = new URLRequest("CheerfulSong.mp3");
var mySoundChannel:SoundChannel = new SoundChannel();
mySound.load(myURL);
play_btn.addEventListener(MouseEvent.CLICK, playSound);
function playSound(e:MouseEvent):void
{
     mySoundChannel = mySound.play();
}mySoundChannel.stop();
This will stop whatever sound that is playing inside the mySoundChannel object.
Let's put this stop statement inside a mouse click event handler for the stop button (stop_btn) so that our sound will stop playing when the user clicks on the stop button:
play_btn.addEventListener(MouseEvent.CLICK, playSound);
stop_btn.addEventListener(MouseEvent.CLICK, stopSound);
function playSound(e:MouseEvent):void
{
     mySoundChannel = mySound.play();
}
function stopSound(e:MouseEvent):void
{
     mySoundChannel.stop();
}NOTE: If you want to be able to play multiple  sounds simultaneously and still be able to individually stop each sound,  then you will need to create one SoundChannel for each sound.
Detecting when a Sound has Reached the End of Playback
In some cases, you might want to enable Flash to detect when a sound has finished playing. Let's say, for example, you have a song that's playing in the background, and you'd like Flash to display an image or a message only when the song has finished. To be able to detect when a sound file has reached the end, you can use the Event.SOUND_COMPLETE event of the SoundChannel class. This event gets dispatched once a sound file completes playback.Let's go ahead and add an Event.SOUND_COMPLETE listener to our code. I'm first going to create the event listener function, which I will name endOfSound.
function playSound(e:MouseEvent):void
{
     mySoundChannel = mySound.play();
}
function stopSound(e:MouseEvent):void
{
     mySoundChannel.stop();
}
function endOfSound(e:Event):void
{
     trace("Sound playback is complete.")
}Now that we have this event listener function, we then need to add the event listener to our SoundChannel object. When you're adding an Event.SOUND_COMPLETE listener, the event listener must only be added AFTER the play sound statement has been assigned to a SoundChannel (i.e. after the SoundChannel = Sound.play(); statement). In our example, this line would be:
mySoundChannel = mySound.play();
And in our code, this line can be found inside the playSound event listener function. So we must add the event listener for Event.SOUND_COMPLETE right after that line:
function playSound(e:MouseEvent):void
{
     mySoundChannel = mySound.play();
     mySoundChannel.addEventListener(Event.SOUND_COMPLETE, endOfSound);
}
function stopSound(e:MouseEvent):void
{
     mySoundChannel.stop();
}
function endOfSound(e:Event):void
{
     trace("Sound playback is complete.")
}Stopping All Sounds at Once
If you have multiple sound clips playing at the same time, you can stop all of them at once by calling the stopAll() method of the SoundMixer class. In the example below, we have some code that stops all the sound clips that are currently playing, when a button is clicked:// Assume that stopAll_btn is a button 
// that already exists on the stage
stopAll_btn.addEventListener(MouseEvent.CLICK, beQuiet);
function beQuiet(e:MouseEvent):void
{
     SoundMixer.stopAll();
}And those are the basics of how you can use the AS3 Sound class and the AS3 SoundChannel class in order to work with sound in Flash.
sumber: http://www.trainingtutorials101.com/2011/01/as3-sound.html
TERIMA KASIH ATAS KUNJUNGAN SAUDARA
Judul: tutorial dasar sound flash AS3
Ditulis oleh Unknown
Rating Blog 5 dari 5
Semoga artikel ini bermanfaat bagi saudara. Jika ingin mengutip, baik itu sebagian atau keseluruhan dari isi artikel ini harap menyertakan link dofollow ke https://cintafido.blogspot.com/2012/08/tutorial-dasar-sound-flash-as3.html. Terima kasih sudah singgah membaca artikel ini.Ditulis oleh Unknown
Rating Blog 5 dari 5
 

 
 
0 komentar:
Posting Komentar