Skip to main content
March 9, 2009
Question

Total Newb, adding sound with Action Script

  • March 9, 2009
  • 6 replies
  • 422 views
Hello, thanks for reading,

I am very very new to flash...I created a quick little slide show...in which I want to add to add an mp3 file too...the slide show works as such..

Each frame has a picture and next button, so when I test the movie. The pictures are stopped, I click the next button and it goes to the next frame......pretty straight forward.

How do I add sound? I just want the music to start when I open it, and stop when the slide show is over.

The funny thing is...if I insert music into a file with no buttons and just let it stream (and stop on the last frame) it works great, the music plays...Only now that I have a movie with buttons, it doesn't even start.

I have been thumbing around the actions panel, but for the most part I'm lost.....

Any help would be greatly appreciated...Thank you very much....

Any help would be great.
This topic has been closed for replies.

6 replies

xchanin
Participating Frequently
March 9, 2009
That is true, so... channel.stop(); will force the sound to end.

Thanks kglad for pointing that out.
kglad
Community Expert
Community Expert
March 9, 2009
don't use an enterframe loop to call a function once.

if you created a soundchannel instance (and, if you didn't you should now) when you applied the play() method to your sound instance, you can apply a stop() to the soundchannel instance to stop your sound when the last frame is played.
xchanin
Participating Frequently
March 9, 2009
If you're using my suggested code, on your last frame you can call the stopMusic() method. something like:

addEventListener(Event.ENTER_FRAME,stopMusic);
March 9, 2009
Hey guys thanks very much....and sorry but not sure what to do or where to put that much code......but Kglad, I did what you suggested ( I think) and added the onload code in the Actions panel....and now I have so that when the movie starts, the music starts...(which is great, half the battle)

But now when I get to the last frame, and click the next button (which goes back to the first frame) the music keeps going...and then starts over, so the one file is running twice...

How do I get it so that it stops playing and starts over when I click the next button on the last slide Sounds kind of funny the same music playing twice at the same time.

Thanks a lot!
xchanin
Participating Frequently
March 9, 2009
package
{
import flash.display.MovieClip;
import flash.events.*;
import flash.media.*;
import flash.net.URLRequest;

public class SoundLoader02 extends MovieClip
{
private var isPlaying:Boolean = false;
private var snd:Sound = new Sound();
private var channel:SoundChannel = new SoundChannel();
private var pos:Number = 0;
private var soundVolume:Number = 1;

public function SoundLoader02()
{
snd.load(new URLRequest("song.mp3"));
btn_Stop.addEventListener(MouseEvent.CLICK, stopMusic);
btn_Stop.buttonMode = true;

btn_Play.addEventListener(MouseEvent.CLICK, playMusic);
btn_Play.buttonMode = true;
}

private function stopMusic(evt:Event):void
{
channel.stop();
pos = 0;
isPlaying = false;
}

private function playMusic(evt:Event):void
{
if(!isPlaying)
{
channel = snd.play(pos);
btn_Play.visible = false;
btn_Pause.visible = true;
isPlaying = true;
}
}
}
}
kglad
Community Expert
Community Expert
March 9, 2009
check the sound class for sample code on loading your sound and playing it.