Discussion:
Music won't stop when I load a new page
(too old to reply)
doghouseJim
2009-03-27 07:13:57 UTC
Permalink
'm building a web site that uses swf's as pages. The menu.swf has 5 buttons
one for each page. When I press a button it loads a page with addChildAt(page,
0); so that the menu is always a level above the currently loaded page. When
one of the pages open it plays a mp3 in a loop. The music keeps playing when
another page loads. The page that was playing the music was also on level 0 so
it's gone yet the music keeps playing. How do I make it so the music stops when
another page loads?
_xchanin
2009-03-27 15:44:19 UTC
Permalink
Did you see the other post about this?
doghouseJim
2009-03-27 16:49:08 UTC
Permalink
I saw the other post so far nothing's helped so far. I was hoping there is a
way for a swf to know it's being unloaded so I could shut things down. Here's
what I tried:

I included a few extra lines of code in the example just so you know I already
have an existing SoundChannel object.

this.addEventListener(Event.UNLOAD, exit);
function exit():void
{
start_snd.removeEventListener(MouseEvent.CLICK, playSound);
sound.removeEventListener(Event.COMPLETE, soundLoaded);
soundControl.stop();
}
// ***************sound stuff********************
var soundReq:URLRequest = new URLRequest("images/Willie Nelson - On the Road
Again.mp3");
var sound:Sound = new Sound();
var soundControl:SoundChannel = new SoundChannel();
_xchanin
2009-03-27 18:38:16 UTC
Permalink
You could always call the exit function on the button clicks.
doghouseJim
2009-03-28 05:33:58 UTC
Permalink
I tried communicating to the SoundChannel object in the child swf no luck
there, I was able to unload the child from the parent swf with both
loader.unload(); and this.removeChildAt(0); but I still have the same the music
just keeps playing.
_xchanin
2009-03-30 14:09:05 UTC
Permalink
So you're not using an external class to build out your audio player?
doghouseJim
2009-03-30 16:00:35 UTC
Permalink
correct it's all on the timeline.
_xchanin
2009-03-30 16:22:13 UTC
Permalink
I just created a simple audio player using the timeline and I'm able to stop
the sound. Here is the code I used for it:

import flash.events.*;
import flash.media.*;
import flash.net.URLRequest;


var isPlaying:Boolean = false;
var snd:Sound = new Sound();
var channel:SoundChannel = new SoundChannel();
var pos:Number = 0;
var soundVolume:Number = 1;


snd.load(new URLRequest("someMP3.mp3"));
btn_Stop.addEventListener(MouseEvent.CLICK, stopMusic);
btn_Stop.buttonMode = true;

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


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

function playMusic(evt:Event):void
{
if(!isPlaying)
{
channel = snd.play(pos);
btn_Play.visible = false;
btn_Pause.visible = true;
isPlaying = true;
}
}
doghouseJim
2009-03-30 18:18:56 UTC
Permalink
_xchanin thanks for stopping by to help. My problem isn't stopping sound it's
stopping sound from the swf that I loaded the swf that's playing the sound
from. The parent swf is called menu.swf all that's there are some buttons.
The buttons load child swf's on level 0 leaving the buttons on level 1. I have
3 swf's so far menu.swf home.swf, and about.swf. From menu.swf if I click home
then home.swf loads on level 0, if I click about then about.swf loads on level
0 and home.swf disappears which is what I want. When about.swf loads it has
some graphics and music if I then click home the home.swf loads replacing about
and everything works just fine except the sound doesn't stop. I've been trying
to modify the buttons so they go into the currently loaded swf and turn off the
sound before loading the next swf.
doghouseJim
2009-03-30 18:39:32 UTC
Permalink
I put what I have so far on the web so you can see. http://charliegooddog.org/
_xchanin
2009-03-30 19:59:49 UTC
Permalink
Oh, I miss-understood what you were saying. You should be able to just unload the .swf you load in: loader.unload();
doghouseJim
2009-03-30 20:35:12 UTC
Permalink
Been there done that. It has the same effect as replacing with another swf on level 0 ie. the swf is gone but the music plays on.
_xchanin
2009-03-30 21:46:00 UTC
Permalink
I wonder if you have to use _parent or something.
Lab860
2009-03-31 02:52:10 UTC
Permalink
Use the unload() event
doghouseJim
2009-03-31 03:50:51 UTC
Permalink
unload removes the swf but don't stop the music this global command does
SoundMixer.soundTransform = new SoundTransform(0); it actually pauses it and
then I have to restart with SoundMixer.soundTransform = new SoundTransform(1);
and I end up with music playing over itself
_xchanin
2009-03-31 14:20:20 UTC
Permalink
What about:
import flash.media.SoundMixer;
SoundMixer.stopAll();
doghouseJim
2009-03-31 15:56:24 UTC
Permalink
Thanks _xchanin you're a lifesaver. Been working with AS3 for long?
_xchanin
2009-03-31 17:30:51 UTC
Permalink
Did this work for you then?

import flash.media.SoundMixer;
SoundMixer.stopAll();

Continue reading on narkive:
Loading...