7 Replies Latest reply on Dec 6, 2016 12:44 PM by christiang7637298

    Can't stop sound using javascript.

    carsonandrew2

      Hello I'm trying to use javascript to start a sound and then stop it when a button is pressed.

      I've tried everything and still can't get it to work.

      using html5 canvas

        • 1. Re: Can't stop sound using javascript.
          Colin Holgate MVP & Adobe Community Professional

          createjs.Sound.stop() should stop all sounds. It should be able to take the id of the sound too, if you want to stop a particular sound:

           

          createjs.Sound.stop(soundID)

          1 person found this helpful
          • 2. Re: Can't stop sound using javascript.
            christiang7637298

            Thanks Colin,

             

            For some reason the stop always stops all the audio and not just the one I have the ID listed for.

             

            Is anyone else having an issue with this?

             

            I can start the sounds individually and stop ALL the sounds but what I really need is to stop just one sound.

             

            I have two buttons (4 if you count the start and stop buttons).

             

            var jsObject = this;

             

            jsObject.audioBtnPlay.addEventListener("click", audioCtrlPlay.bind(jsObject));

            jsObject.audioBtnPause.addEventListener("click", audioCtrlPause.bind(jsObject));

             

            jsObject.audioBtnPlay2.addEventListener("click", audioCtrlPlay2.bind(jsObject));

            jsObject.audioBtnPause2.addEventListener("click", audioCtrlPause2.bind(jsObject));

             

            jsObject.audioBtnPlay.visible = true;

            jsObject.audioBtnPause.visible = false;

             

            createjs.Sound.registerSound("sounds/musicfile.mp3", "sound_id");

            createjs.Sound.registerSound("sounds/audioTwo.mp3", "sound_id2");

             

            function audioCtrlPlay() {

              createjs.Sound.play("sound_id");

              jsObject.audioBtnPlay.visible = false;

              jsObject.audioBtnPause.visible = true;

            }

            function audioCtrlPause() {

              createjs.Sound.stop("sound_id");

              jsObject.audioBtnPlay.visible = true;

              jsObject.audioBtnPause.visible = false;

            }

            function audioCtrlPlay2() {

              createjs.Sound.play("sound_id2");//

              jsObject.audioBtnPlay2.visible = false;

              jsObject.audioBtnPause2.visible = true;

            }

            function audioCtrlPause2() {

              createjs.Sound.stop("sound_id2");

              jsObject.audioBtnPlay2.visible = true;

              jsObject.audioBtnPause2.visible = false;

            }

             

            Please don't tell me it's just be who has this issue.

             

            Cheers,

             

            Christian

            • 3. Re: Can't stop sound using javascript.
              ClayUUID Adobe Community Professional

              Colin Holgate wrote:

               

              createjs.Sound.stop() should stop all sounds. It should be able to take the id of the sound too, if you want to stop a particular sound:

               

              createjs.Sound.stop(soundID)

              Incorrect. Sound.stop() performs a global stop only. To "stop" a specific sound, you have to set the paused property on it.

               

              Is there a pause() method? · Issue #200 · CreateJS/SoundJS · GitHub

              • 4. Re: Can't stop sound using javascript.
                Colin Holgate MVP & Adobe Community Professional

                Would you use setPaused(boolean)? Actually, reading about it, it seems there's a neat way to do getters and setters, if I read this right:

                 

                  Object.defineProperty(this, "paused", {

                  get: this.getPaused,

                  set: this.setPaused

                  });

                 

                That way you can read or set the paused state just by talking to 'paused' instead of using setPaused(boolean) and getPaused().

                • 5. Re: Can't stop sound using javascript.
                  ClayUUID Adobe Community Professional

                  No, you wouldn't use setPaused(boolean). You'd do what I just said-- set the value of the .paused property directly to true or false.

                   

                  In fact, in the linked discussion gskinner explicitly says "we deprecated setPaused/getPaused". So nobody even think about using those.

                  • 6. Re: Can't stop sound using javascript.
                    Colin Holgate MVP & Adobe Community Professional

                    I was saying that you don't need to do setPaused(boolean) because the internal SoundJS code I showed takes care of that for you.

                    • 7. Re: Can't stop sound using javascript.
                      christiang7637298 Level 1

                      Thanks very much. This is great.