954,598 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Stop sound from automatically playing?

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;
}
}
snitch321
Junior Poster in Training
63 posts since Nov 2007
Reputation Points: 9
Solved Threads: 2
 

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.

JasonHippy
Master Poster
772 posts since Jan 2009
Reputation Points: 590
Solved Threads: 125
 

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

snitch321
Junior Poster in Training
63 posts since Nov 2007
Reputation Points: 9
Solved Threads: 2
 

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

snitch321
Junior Poster in Training
63 posts since Nov 2007
Reputation Points: 9
Solved Threads: 2
 

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.

JasonHippy
Master Poster
772 posts since Jan 2009
Reputation Points: 590
Solved Threads: 125
 

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.

snitch321
Junior Poster in Training
63 posts since Nov 2007
Reputation Points: 9
Solved Threads: 2
 

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.

shwetahirday
Newbie Poster
1 post since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

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

snitch321
Junior Poster in Training
63 posts since Nov 2007
Reputation Points: 9
Solved Threads: 2
 

solution: dont put sounds on the web page,

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

almostbob
Posting Sensei
3,149 posts since Jan 2009
Reputation Points: 571
Solved Threads: 376
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: