want to know what code snippet you could add to this code to stop my sound file from playing automatically when you enter that frame?

code:

var mySound:Sound = new Sound();
var myChannel:SoundChannel = new SoundChannel();
var lastPosition:Number = 0; 
var soundIsPlaying:Boolean = true;
mySound.load(new URLRequest("Intro1.mp3"));

myChannel = mySound.play();
offBtn.addEventListener(MouseEvent.CLICK, onClickStop);
function onClickStop(e:MouseEvent):void{
myChannel.stop();
soundIsPlaying = false;
} 
offBtn.addEventListener(MouseEvent.CLICK, onClickPause);
function onClickPause(e:MouseEvent):void{
lastPosition = myChannel.position;
myChannel.stop();
soundIsPlaying = false;
}
onBtn.addEventListener(MouseEvent.CLICK, onClickPlay);
function onClickPlay(e:MouseEvent):void{
if(!soundIsPlaying){
myChannel = mySound.play(lastPosition);
soundIsPlaying = true;
}
}

Recommended Answers

All 8 Replies

This should be pretty self-explanatory. Here's your code again, I've commented out a line of code which should stop your sound from auto-playing:

var mySound:Sound = new Sound();
var myChannel:SoundChannel = new SoundChannel();
var lastPosition:Number = 0; 
var soundIsPlaying:Boolean = true;
mySound.load(new URLRequest("Intro1.mp3"));

//Line below not needed:
//myChannel = mySound.play();
// after removing this, the sound will not play until the 
// play button has been clicked.
offBtn.addEventListener(MouseEvent.CLICK, onClickStop);
function onClickStop(e:MouseEvent):void{
myChannel.stop();
soundIsPlaying = false;
} 
offBtn.addEventListener(MouseEvent.CLICK, onClickPause);
function onClickPause(e:MouseEvent):void{
lastPosition = myChannel.position;
myChannel.stop();
soundIsPlaying = false;
}
onBtn.addEventListener(MouseEvent.CLICK, onClickPlay);
function onClickPlay(e:MouseEvent):void{
if(!soundIsPlaying){
myChannel = mySound.play(lastPosition);
soundIsPlaying = true;
}
}

Cheers for now,
Jas.

thanks for that. but i have a problem now where the when i go to another page the sound files when played overlap?

I read up that playing external sounds causes this overlapping glitch. So maybe you could give me some code to play the sound from the library?

thank you jason

You could try using SoundMixer.stopAll();

So when the user navigates to a new page, ensure that SoundMixer.stopAll() is called before the new sound is started. That should stop any sounds that are currently playing; regardless of whether they're external or in the library.

To use SoundMixer, you'll need to use the following import:

import flash.media.SoundMixer;

Cheers for now,
Jas.

well i actually abandoned this idea of playing sound because i added your code yet the sound still overlaps. It's weird, when i move to a new frame and the sound automatically begins playing its fine, but when i then stop it and play it manually it then overlaps. so if you have a solution it would be good but dont try too hard since im abandoning this idea.

good one.....................................

This should be pretty self-explanatory. Here's your code again, I've commented out a line of code which should stop your sound from auto-playing:

var mySound:Sound = new Sound();
var myChannel:SoundChannel = new SoundChannel();
var lastPosition:Number = 0; 
var soundIsPlaying:Boolean = true;
mySound.load(new URLRequest("Intro1.mp3"));

//Line below not needed:
//myChannel = mySound.play();
// after removing this, the sound will not play until the 
// play button has been clicked.
offBtn.addEventListener(MouseEvent.CLICK, onClickStop);
function onClickStop(e:MouseEvent):void{
myChannel.stop();
soundIsPlaying = false;
} 
offBtn.addEventListener(MouseEvent.CLICK, onClickPause);
function onClickPause(e:MouseEvent):void{
lastPosition = myChannel.position;
myChannel.stop();
soundIsPlaying = false;
}
onBtn.addEventListener(MouseEvent.CLICK, onClickPlay);
function onClickPlay(e:MouseEvent):void{
if(!soundIsPlaying){
myChannel = mySound.play(lastPosition);
soundIsPlaying = true;
}
}

Cheers for now,
Jas.

this seems to be a classic problem that i have never seen a solution that works and still no luck i'm afraid.

solution: dont put sounds on the web page,

there are enough annoying useless crap sites, there is no requirement for any more

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.