# Problem with sound doubling up when playing external MP3 files

**URL:** <https://forum.kirupa.com/t/problem-with-sound-doubling-up-when-playing-external-mp3-files/297096>\
**Category:** flash\
**Created:** [September 25, 2009, 1:38pm UTC](https://forum.kirupa.com/t/problem-with-sound-doubling-up-when-playing-external-mp3-files/297096 "2009-09-25T13:38:59Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![linkolisa](https://avatars.discourse-cdn.com/v4/letter/l/b4bc9f/32.png) [@linkolisa](https://forum.kirupa.com/u/linkolisa)\
**Post date:** [September 25, 2009, 1:38pm UTC](https://forum.kirupa.com/t/problem-with-sound-doubling-up-when-playing-external-mp3-files/297096/1 "2009-09-25T13:38:59Z")

</div>

I’ve created an audio player with actionscript 3 which plays one of 55 different sounds, depending on a variable set from which button a user clicks on. It works fine for the first 1 or 2 plays, but after that the sound begins to double-up, or even triple-up. I’m using the code below.

//First I declare a few variables:

[INDENT] var sound:Sound;  
var controller:SoundChannel;  
var pausePos:Number = 0;  
var audioReset:Boolean = false;  
var playerObject:Object = new Object();  
[/INDENT]  
//The sound functions are placed within another function so that it can be called again once a new button is selected:

[INDENT] function audiofunc() {

//if the user is selecting a second audio file, it runs this "audioReset function to try to clear the EventListeners  
if (audioReset == true) {  
audio.play\_btn.removeEventListener(MouseEvent.CLIC K, playSound);  
audio.pause\_btn.removeEventListener(MouseEvent.CLI CK, stopSound);  
playerObject.sound.removeEventListener(Event.COMPL ETE, soundLoaded);  
playerObject.sound.removeEventListener(ProgressEve nt.PROGRESS, preloadAudio);  
delete playerObject.sound;  
flash.media.SoundMixer.stopAll(); //stop all sounds that might be playing  
controller = null;  
}

audio.audio\_loader.visible = true; //show the loading screen over the button  
audio.pause\_btn.visible = false; //hide pause button  
audio.play\_btn.visible = true; //show play button

var audioreq:URLRequest = new URLRequest (partnerOn+"\_audio.mp3");  
playerObject.sound = new Sound();

playerObject.sound.addEventListener(Event.COMPLETE , soundLoaded, false, 0, true);  
playerObject.sound.addEventListener(ProgressEvent. PROGRESS, preloadAudio, false, 0, true);  
playerObject.sound.load(audioreq);

function preloadAudio(event:ProgressEvent):void {  
var percent2:Number = Math.round (event.bytesLoaded / event.bytesTotal \* 100);  
audio.audio\_loader.audio\_preload\_txt.text = String(percent2) + " %";  
}

function soundLoaded(event:Event):void {  
controller = playerObject.sound.play();  
controller.stop();  
audio.audio\_loader.visible = false;  
audio.play\_btn.addEventListener(MouseEvent.CLICK, playSound, false, 0, true);  
audio.pause\_btn.addEventListener(MouseEvent.CLICK, stopSound, false, 0, true);  
}

function playSound(event:MouseEvent):void {  
controller = playerObject.sound.play(pausePos);  
audio.play\_btn.visible = false;  
audio.pause\_btn.visible = true; //show pause button  
}

function stopSound(event:MouseEvent):void {  
pausePos = controller.position;  
controller.stop();  
flash.media.SoundMixer.stopAll();  
trace(pausePos);  
audio.play\_btn.visible = true; //show play button  
audio.pause\_btn.visible = false;  
}  
}

audiofunc();  
[/INDENT]   
I’m assuming that somehow an EventListener isn’t getting removed, so it’s causing a few versions of the audio file to play at once. If I trace the value of pausePos, it starts to appear in multiples (the same number, but 2, 3 or 4 times) after a few plays of the different audio files.

If anyone has any suggestions, I would be most grateful.
