Here some info about how to create a movie with swishmax and to let the movie load and play an external MP3 music file.
There are several ways – such as Using the “Soundtract” method in Swishmax or by scripting in “Play Sound.”
This short note will explain how to load/play sound/music files dynamically using Action Script in Swishmax.
The benefit of using this method is that you will be able to play music dynamically without increasing your file size. Also, when using this method.... your music does not get downloaded into the viewer's cache... as far as I know.
When using what is called the “Sound Object” method in Swishmax it is necessary to export to SWF6 mode and use only mp3 files.
Let’s get started:
***************************************************************
Using Action Script to play music/sounds requires you to export you movie as SWF6
***************************************************************
The first thing we need to do is to create your sound object in Action Script. To do this place or type this script into your movie, say in Frame 3:
onFrame (3) {
mySound = new Sound();
mySound.loadSound("song1.mp3");
mySound.start(0, 5);
}
In this example above the mySond=new Sound(); established the sound object. This created sound object will be recognized by the Flash Player.
The next line of script mySound.loadSound(“song1.mp3”); tells the sound object via the Flash Player to now load the sound called “song1.mp3.”
Note: the sound object method will only use mp3 and not .wav files.
The next line of script mySound.start(0, 5); tells the sound object to start playing the sound file. The (0, 5) indicates that it will loop this sound file 5 times.
When using the above script -- it works great for a CD presentation. However, over the internet there is sometimes difficulty in getting the music to start automatically.
To solve - over the internet- problem we just add another script called Sound.onLoad();
Basically this tells the Sound Object/Flash Player that when the music file is loaded to go and play it. The script would look like this:
onFrame (3) {
mySound = new Sound();
mySound.onLoad = function(sucess){
if (sucess)
mySound.start(0, 99);
else
displayLoadError();
};
mySound.loadSound("yourmusicfile.mp3");
}
The first line of script onFrame (3) { establishes what frame this script will function
The second line of code mySound = new Sound(); creates the Sound Object
The third line mySound.onLoad = function(sucess){ creates a function using the Sound.onLoad handler that tells the Flash Player that if the music is loaded (sucess) then start playing the music (mySound.start(0, 99);
The last line of code mySound.loadSound("yourmusicfile.mp3"); of course loads the sound into the Sound Object. While this is happening the on.Load handler/function is, for lack of a better word, watching to see if this "loading" will be a sucess.... and if so then it will play the sound.
Below is an explanation as to how to stream a sound/music over the internet. If you use the streaming script below you won't need to use the Sound.on.Load script mentioned above.
If you want this sound to be a streaming sound from the internet then you could change the script slightly and by adding the word true and a comma before it. It would look like this:
onFrame (3) {
mySound = new Sound();
mySound.loadSound("song1.mp3", true);
mySound.start();
}
Notice that in the line mySound.start() that the 0, 5 is missing. The reason for this is because according to Colin Moock the Flash Player will not loop streaming sound using this method.
However, if you want the streaming sound to loop just use the onSoundComplete() script to replay the sound object and it will play forever.
Now if you would like you movie to do something when the sound/music is completed this can be accomplished by adding some additin script.
By using the onSoundComplete() handler…. You could go to a scene or tell the movie to do something when the music/sound is completed. The script would look like this:
on (release) {
mySound=new Sound();
mySound.loadSound("testmusic.mp3");
mySound.start("0,1");
mySound.onSoundComplete = function () {
gotoAndPlay("testing");
}
}
In this example above the music is started with the click of a button and when the music is completed the Flash Player will then go to the label testing in Scene1 and play the animation.
on (release) {
mySound=new Sound();
mySound.loadSound("testmusic.mp3");
mySound.start("0,1");
mySound.onSoundComplete = function () {
gotoAndPlay("Scene_2",2);
}
}
In this example when the music is completed the Flash Player will go to Scene2 and frame 2 and begin playing.
If you want to pause and restart your music from where it left off – then you would need a way for the Flash Player to remember where the music stopped.
When play is pressed again it would continue from that point. This can be done with just a little bit of Action Script. It would look like this:
On frame 2 of your main scene you would place this script to establish/create your sound object:
onFrame (2) {
mySound=new Sound();
mySound.loadSound("testmusic.mp3");
mySound.start(0, 5);
}
In your movie you would have three buttons: stop, play, and pause. The scripts for these would be as follows:
On the Stop button:
on (press) {
mySound.stop();
mySoundPosition=0;
}
This simply stops the sound and resets the sound object to the beginning of the sound – position 0
On the Pause button:
on (press) {
mySoundPosition=_root.mySound.position/1000;
mySound.stop();
}
The second line of script tells the Flash Player to remember the position in terms of where in the song the “pause” button was pressed…. And then stop the music.
On the Play button:
on (press) {
mySound.start(mySoundPosition,0);
}
The second line of this script tells the Flash Player to now begin playing the music at the paused postion.
Setting the Volume and Changing the Volume of a Sound Object
Many people have asked how they can set and change the volume of their sound/music. This can easily be done using the setVolume() script in your Action Script.
The first step would be to establish the initial volume level of your Sound/music...you would do this when you create your Sound Object (script) in your movie. The script would look something like this:
onFrame (3) {
mySound = new Sound();
mySound.loadSound("song1.mp3");
mySound.setVolume(100);
mySound.start();
}
Notice - mySound.loadSound("song1.mp3"); This is not streaming sound/music. To make it streaming just change it to: mySound.loadSound("song1.mp3", true); See earlier part of this tutorial.
And for streaming sound/musc you can leave out the mySound.start(); . The sound/music will automatically start without it.
Notice again in the above script - the mySound.setVolume(100);. This will set your initial volume to 100 which is full volume. This is needed here so you can, later in the movie, change the volume to your desired level.
Now... say you want to re-set the volume to a lower value further into your movie, say on frame 95.... then you would add the script below on frame 95:
onFrame (95){
mySound.setVolume(20);
}
This will lower your volume of the Sound Object (not the PC speakers) to a value of 20.
Also.... after you have created your Sound Object..as noted above (mySound= new Sound etc) ..... if you want your volume gradually decreased to a "mute" value of 0 then you could just place the setVolume() script above with the desired value (0-100) in a series in your timeline.
Thus the volume will decrease as the movie continues to play from frame to frame. Your script might look something like this:
onFrame (95){
mySound.setVolume(80);
}
onFrame (110){
mySound.setVolume(60);
}
onFrame (120){
mySound.setVolume(40);
}
onFrame (130){
mySound.setVolume(20);
}
onFrame (140){
mySound.setVolume(10);
}
onFrame (150){
mySound.setVolume(0);
}
onFrame (160){
mySound.stop();
}
onFrame (170) {
stop();
}
The mySound.stop(); on frame 160 can be used to reduce CPU usage during Sound "Mute" setting .... thanks Lee.
Remember that a setVolume(0) ... 0 setting is mute and not stop. The music will stop playing (although in "mute" mode) on frame 160, in the example script above, because of the mySound.stop(); script.
Note: The setVolume() values are only samples and you will need to experiment with the values to obtain the optimum sound effects and smoothest transition.
There are other ways of gradually decreasing/adjusting the volume of a Sound Object, but this seems to be the most popular/easier way to do it.
Remember that depending on your specific application of the scripts above slight modifications might be necessary to account for in sprite uses, and other specific congigurations etc.
Wednesday, September 24, 2008
Subscribe to:
Posts (Atom)
