FORUMS: list search recent posts

Flash CS3 music/sound question

COW Forums : Adobe Flash

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Nathan Maynard
Flash CS3 music/sound question
on Jul 25, 2011 at 4:49:51 am

I have a sound-related question that seems like it would be exceedingly simple to do, yet all my sound-related searches turn up either methods not viable in my version of Flash, or sound manipulation techniques that don't do what I need.

Here's the scenario: My flash animation has a background music that plays throughout, as well as sound effects that play at specific points in the movie. The background music should be playing from the start, but I need a button that will toggle the music on and off without disrupting the sound effects; it needs to turn on and off the background music specifically.

The file, "song.mp3", is in the same folder as the flash project.

I'm imagining the actionscript to accomplish this should look something like this:
songbutton.buttonMode = true;
songbutton.addEventListener(MouseEvent.MOUSE_DOWN, onSongClick);

var songBoolean:Boolean = true;

var bgm:Sound = new Sound;

function onSongClick(e:MouseEvent):void
if(songBoolean == true)
songBoolean = false;
songBoolean = true;

Any suggestions?

Return to posts index

demetri tashie
Re: Flash CS3 music/sound question
on Jul 26, 2011 at 7:35:51 pm

you're almost there, but not quite.

first, you should have your Sound play through a SoundChannel. Keeping your different sounds
distinct, played through separate SoundChannels is important, especially if you want to manipulate them separately.

the sequence is this: load the URLRequest ( the mp3) into the instantiated Sound Object, create a SoundChannel, and then have the SoundChannel play that Sound Object

the logic in your 'if' statement is good, it just now needs to reflect correct coding for a sound channel.

If you are expecting to actually just 'pause' the Sound, know that you can really only just stop it. it automatically 'rewinds' to the beginning. so when you start it again it will play from the beginning. if that is ok, fine. if not, you need to somehow take note of where the SoundChannel's position was when you stopped it, and then let it start up not from the beginnng, but from that new point.
you can accomplish that by just asking for the SoundChannel position, then telling it to start from that position.

here is some complete code i wrote a while ago that does all this. i updated the code to reflect the names that you have chosen to use.

var backgroundMusic:URLRequest=new URLRequest("song.mp3");
var bgm:Sound = new Sound();

var sndChannel:SoundChannel = new SoundChannel();;

var pos:Number; // this will be used for the SoundChannel position;
var songBoolean:Boolean=true; // keeps track of if the music is playing.

songbutton.addEventListener(MouseEvent.CLICK, onSongClick);

function onSongClick(e:MouseEvent):void {

if (songBoolean) {
else if (!songBoolean) {; // plays from the last position place it was stopped (pos)

hope that helps. let me know if anything needs clarification.

Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2018 All Rights Reserved