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;
}
}

Dani AI

Generated

A short diagnosis and a practical fix based on the thread: the overlap isn’t a Flash “mystery” so much as a scope/listener lifecycle problem. ’s symptoms (sound plays once on frame entry but later manual play causes overlap) match these common causes: the sound/channel is being re-created each time you visit a frame, event listeners get added multiple times, and the code’s initial state (a boolean set to true) can prevent correct start/stop logic. was right to point out removing the auto-play line and suggesting a global stop call, but you also need a single controller that lives across frames or a guard so the player code only runs once.

Recommended fixes (in order):

  • Initialize your “is playing” state correctly (false if you remove autoplay).
  • Avoid adding the same listeners repeatedly (check a flag or add listeners from a single, persistent controller).
  • Always stop the existing channel before you call play again (that’s what prevents overlap).
  • Use a single Sound manager (Document class, singleton, or a MovieClip on frame 1 that never gets reinitialized) so there’s only one SoundChannel in play.

Example: a small singleton SoundManager you can put in a Document class (keeps one channel, exposes play/pause/stop):

package {
  import flash.media.*;
  import flash.net.*;
  public class SoundManager {
    private static var _inst:SoundManager;
    private var snd:Sound;
    private var ch:SoundChannel;
    private var pos:Number = 0;
    public function SoundManager(enforcer:SingletonEnforcer) {}
    public static function getInstance():SoundManager {
      if (!_inst) _inst = new SoundManager(new SingletonEnforcer());
      return _inst;
    }
    public function loadUrl(url:String):void { snd = new Sound(new URLRequest(url)); }
    public function setSound(s:Sound):void { snd = s; }
    public function play():void { if (!snd) return; if (ch) ch.stop(); ch = snd.play(pos); }
    public function pause():void { if (!ch) return; pos = ch.position; ch.stop(); ch = null; }
    public function stop():void { if (ch) ch.stop(); ch = null; pos = 0; }
  }
}
class SingletonEnforcer {}

If you prefer the library route (asked earlier): export the sound for ActionScript, instantiate it once (new YourLibrarySound()) and hand it to the manager above — that avoids load timing oddities. Finally, for debugging, trace channel creation and check that listeners are not added twice; use REMOVED_FROM_STAGE handlers or explicit removeEventListener when you leave a frame.

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, networking, learning, and sharing knowledge.