Skip to main content
edwinmerced
Inspiring
November 8, 2012
Answered

Assuring a single instance of sound

  • November 8, 2012
  • 1 reply
  • 1331 views

I have this code:

var mySound:Sound = new Sound();

var myChannel:SoundChannel = new SoundChannel();

var lastPosition:Number = 0;

mySound.load(new URLRequest("audio/audio1.mp3"));

pause_btn.addEventListener(MouseEvent.CLICK, onClickPause);

function onClickPause(e:MouseEvent):void{

    lastPosition = myChannel.position;

    myChannel.stop();

}

play_btn.addEventListener(MouseEvent.CLICK, onClickPlay);

function onClickPlay(e:MouseEvent):void{

    myChannel = mySound.play(lastPosition);

}

stop_btn.addEventListener(MouseEvent.CLICK, onClickStop);

function onClickStop(e:MouseEvent):void{

     SoundMixer.stopAll();

}

Everything works fine : When I click the play button it starts and resumes sund. But if  I accidently press the start buttun twice it brings on two simultaneos sounds. How can I make sure that only 1 instance of sound is playing?

Thanks for any help.

This topic has been closed for replies.
Correct answer kglad

use:

var isPlayingBool:Boolean;

var mySound:Sound = new Sound();

var myChannel:SoundChannel = new SoundChannel();

var lastPosition:Number = 0;

mySound.load(new URLRequest("audio/audio1.mp3"));

pause_btn.addEventListener(MouseEvent.CLICK, onClickPause);

function onClickPause(e:MouseEvent):void{

    lastPosition = myChannel.position;

    myChannel.stop();

isPlayingBool=false;

}

play_btn.addEventListener(MouseEvent.CLICK, onClickPlay);

function onClickPlay(e:MouseEvent):void{

if(!isPlayingBool){

    myChannel = mySound.play(lastPosition);

isPlayingBool=true;

}

}

stop_btn.addEventListener(MouseEvent.CLICK, onClickStop);

function onClickStop(e:MouseEvent):void{

     SoundMixer.stopAll();

isPlayingBool=false;

}

1 reply

kglad
Community Expert
Community Expert
November 9, 2012

:

var isPlaying:Boolean;

var mySound:Sound = new Sound();

var myChannel:SoundChannel = new SoundChannel();

var lastPosition:Number = 0;

mySound.load(new URLRequest("audio/audio1.mp3"));

pause_btn.addEventListener(MouseEvent.CLICK, onClickPause);

function onClickPause(e:MouseEvent):void{

    lastPosition = myChannel.position;

    myChannel.stop();

isPlaying=false;

}

play_btn.addEventListener(MouseEvent.CLICK, onClickPlay);

function onClickPlay(e:MouseEvent):void{

if(!isPlaying){

    myChannel = mySound.play(lastPosition);

isPlaying=true;

}

}

stop_btn.addEventListener(MouseEvent.CLICK, onClickStop);

function onClickStop(e:MouseEvent):void{

     SoundMixer.stopAll();

isPlaying=false;

}

edwinmerced
Inspiring
November 9, 2012

Hello KGlad:

I get this error:

Symbol 'audio', Layer 'Layer 2', Frame 1, Line 111152: A conflict exists with inherited definition flash.display:MovieClip.isPlaying in namespace public.

I also changed the variable name but the sound multiplies each time I press the play_btn button.