Stop/Start Sound When Frame Reached

Greetings again! Just wanted to know… I’m using multiple background music in the cartoon that I’m making, but I don’t want to go into all the trouble of actually editting the mp3 using another program. So, how do we make it in flash so that when a certain frame is reached, the specified background music will stop or start, and as well as when a certain button is pressed, the music will stop or start.

Thanks a lot people!=)

Hello NeonGold,

the trick to controlling your sounds with precision is to use ActionScript. Before continuing with an explanation on how this is done, you must make sure that you export your sound from the library. To do this, right-click on the sound you want exported from the library and click Linkage…. When a box appears, check the Export for ActionScript box, and enter a relevant name in the Identifier text box. Once that’s done, you’re ready for some ActionScript :slight_smile:

1. Stopping a sound at a particular frame
Assuming your cartoon is inside a movie clip with the instance name cartoon (and is placed on the _root level) and that you exported your sound as track1, attach the following code to your movie clip:


onClipEvent(load) {
  this.soundTrack = new Sound();
  this.soundTrack.attachSound("track1");
  this.soundTrack.start(0, ***number of loops***);
  this.soundInit = true;
}

onClipEvent(enterFrame) {
  if(this._currentframe == ***frame to stop sound***) {
    this.soundTrack.stop("track1");
  }  
}

That should do it.

2. Starting and stopping sound with a button
Make a button and attach the following code to it (I am assuming that you want to start and stop the sound in your movie clip cartoon):


on(press) {
  if(_root.cartoon.soundTrack.soundInit == false) {
    _root.cartoon.soundTrack.start(0, ***number of loops***);
    _root.cartoon.soundTrack.soundInit = true;
  }

  else {
    _root.cartoon.soundTrack.stop("track1");
    _root.cartoon.soundTrack.soundInit = false;
  }
}

And that’s it!

If you encounter any problems, let me know and I’ll work it out.

:slight_smile:

-ptolemy

how can you turn it off with a button?

my movie will have a main interface and it will load its music from other .swfs.

so I wonder which way would be more effective, take less time, or any other advantages

_root.brian.thank.you