Internet Commerce

Partners & Affiliates

Developer Channel


Featured Flash FLA
Gallery Downloads 11401 Flash Movies | 5 New Flash Movies Added
What's New | Top 100

Featured FLA

»  Author Miguel Panos
»  Title: Tarta
»  Description: It is a circle graph or pie chart that takes the data entered by user
»  More by Miguel Panos


Random FLAs | Add Flash Movie
Featured Flash Site
Gallery Downloads 4839 Flash Sites | 1 New Flash Links
What's New | Top 100 Flash Site

Featured Site

»  AuthorDeft Creative Ltd
»  Link: Home Page
»  Description: Portfolio site for .DeftCreative Ltd. A UK based web design studio specialising in flash websites, games and interactive CDROMs. With an emphasis on making things different.


Random Links | Add your own Flash Related Links
Flash Tutorials 1484 Tutorials 7 New Tutorials Added!
What's New | Top100

» Making Automatic Training Screen Capture Easily
» Create Undersea Life Animation
» Making Deinterlace Video with a low bitrate Easily
» How To Make A Simple Animation Using Christmas Clips
» Simple Step by step flash game tutorial Spot the diffrence
» How To Make A Moving Text Slide
» Create Flash Banner With Text Float Effect
» How To Make Zoo Photos Slideshow
» How To Make A Dolphin Photos Slideshow
» Unknown Tag: Title10
Random Tutorial | Add Site


Tutorials Home What's New Top Rated Submit myTutes Random!

Search Tutorials


Tutorials Tutorials » Audio

Categories How to Use Flash MX Sound Objects
Author: Kenny Bellew | Website: http://www.cowfly.com |

 
Page 19
«prev ... 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 ... next»

How to Invoke Actions When a Sound Object Ends

Fig. 14: Example of invoking an event at the completion of a sound object

In the Flash example in Fig. 14 (to the right), press play, and notice that the text box reports when the sound completes.

The event handler "onSoundComplete" allows you to have actions occur upon completion of a sound object. The syntax for this for a sound object with the instance name of "myMusic" is as follows:

myMusic.onSoundComplete = callbackFunction

Commonly, "onSoundComplete" is used as follows:

myMusic.onSoundComplete = function() {
trace("MyMusic has completed");
}

If you wanted the next dynamically loaded MP3 to load when myMusic completes, the following code could be used:

myMusic.onSoundComplete = function() {
_root.myNextSong.loadSound("sample02.mp3");
_root.myNextSound.start();
}

In the above example, a sound object with the instance name of "myNextSong", which was defined on the _root timeline, would load the file "sample02.mp3" and start playing it as soon as the file "sample.mp3" completed.

The onSoundComplete method does not need to be called in a movie clip that loops or in a play button. Once the frame containing the onSoundComplete code has been processed, the function will activate its commands every time the sound completes. In fact, if the onSoundComplete method is placed in a one frame loop, it may not work, as the loop is constantly redefining the function.

If the sound starts and ends a second time, the onSoundComplete call will continue activating whatever ActionScript was contained within its function definition. If you don't want the function to call the initial onSoundComplete actions, redefine the onSoundComplete with a new function that does whatever is needed (which might be nothing).

The onSoundComplete method was used often in the creation of the examples shown here. If a sound object is started while the same sound object is playing, the sound object will play on top of itself. If this is not intended, a way to keep the play button from restarting the sound while it is playing is needed. Most of the examples set a variable of "playing=true" when the play button is pressed. The play button also looks to see if "playing!=true" before allowing the play code to process. However, when the sound object completes, the "playing" still equals "true", which keeps the play button inactive even when the sound has completed.

One way to resolve this is to use the onSoundComplete method. Note the following code used for the play and stop button to build the example used in this section:

on (press) {
//Play Button
if (playing!=true) {
_root.firstSound.start();
playing=true;
_root.myTextBox="Playing"
}
_root.firstSound.onSoundComplete = function() {
_root.myTextBox="Complete"
playing=false;
}
}

The stop button:

on (press) {
//Stop Button
if (playing==true) {
_root.firstSound.stop("firstSound01");
playing=false;
_root.myTextBox="Stopped";
}
}

The uses for onSoundComplete are numerous. In previous versions of Flash, timing an animation to end at the same time a sound ended must have been tortuous. The speed of the computer or internet connection often determined when the animation ended. Now, there is a way to make sure that animation and sound synchronize. Combine onSoundComplete with the new "position" method of the sound object, and suddenly the animation to sound synchronization possibilities are so much easier and more exact.

«prev ... 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 ... next»

» Level Intermediate

Added: : 2004-10-12
Rating: 8.48 Votes: 31
Hits: 1294
» Author
Kenny Bellew is a technical writer and freelance flash programmer who specializes in Flash audio. He lives in Minneapolis, MN.
» Download
Download the files used in this tutorial.
Download (12927 kb)
Get conversion and unzipping tools for PC and Mac here!

» Forums
More help? Search our boards for quick answers!

Please rate this tutorial, 10 is the top rating, you can also click the comments link to read/write a review.
10 9 8 7 6 5 4 3 2 1
Read or Post Comments