1

I have tried using:

  1. createjs.Sound.pause;

  2. mySound.stop();

Nothing works. Right now it have a stop button but if the stop button is pressed then the sound would not be played again.

This is the soundjs that im using. soundjs

My code is exactly the same with the source code of it. Thanks for any help!

I end up doing it like this Based on Stephen Lightcap answer:

Javascript inside the head tag:

    function load() {
        // Update the UI
        document.getElementById("display").innerText = ".";


        // Load the sound
        createjs.Sound.alternateExtensions = ["mp3"];
        createjs.Sound.addEventListener("fileload", handleFileLoad);
        createjs.Sound.registerSound({id:"mySound", src:"../../test.mp3"});
    }

    function handleFileLoad(event) {
        // Update the UI
        document.getElementById("display").innerHTML = ".";
        document.getElementById("stopBtn").disabled = "";

        // Play the loaded sound
        createjs.Sound.play(event.src,{loop:2});

    }
    var instance = createjs.Sound.play(event.src);
</script>

Button inside the body tag:

<input id="pausBtn" type="button" value="Pause" onclick="pause()"/>

Javascript for the button onclick. Placed below the button. :

<script>
function pause(){
instance.pause();
}
</script>

But I got an error saying :

Uncaught TypeError: Cannot read property 'pause' of undefined at pause (phkk.html:560) at HTMLInputElement.onclick (phkk.html:177)

C.topo
  • 17
  • 6

1 Answers1

0

You can store it in an var and then use the function pause()

var instance = createjs.Sound.play(event.src);

Have button that calls a method that will store the pause

<input id="pausBtn" type="button" value="Pause" onclick="pause()"/>

The JS function:

function pause(){
    instance.pause();
}

EDIT

I went over the docs and it appears it doesn't function like that any more, so you're going to have to use the paused property

Here's a rough outline on how it works for me:

<script>

    var instance; 

    function load() {
        // Update the UI
        document.getElementById("display").innerText = ".";


        // Load the sound
        createjs.Sound.alternateExtensions = ["mp3"];
        createjs.Sound.addEventListener("fileload", handleFileLoad);
        createjs.Sound.registerSound({id:"mySound", src:"../../test.mp3"});
    }

   function handleFileLoad(event) {
       // Update the UI
       document.getElementById("display").innerHTML = ".";
       document.getElementById("stopBtn").disabled = "";

       // Play the loaded sound
       instance = createjs.Sound.play(event.src,{loop:2});

   }

   function pause(){
       instance.paused ? instance.paused = false : instance.paused = true;
   }



</script>

The HTML buttons:

<body>
<input id="loadBtn" type="button" value="Begin Loading" onclick="load()"/>
<input id="stopBtn" type="button" value="Stop Playing" onclick="createjs.Sound.stop();" disabled="disabled" />
    <input id="pauseBtn" type="button" value="Pause" onclick="pause()"/>
<label id="display">Waiting for User Input. Click "Begin Loading".</label>

Stephen
  • 1,072
  • 1
  • 19
  • 33
  • I know this is simple but I would like to know where should I put the `var instance = createjs.Sound.play(event.src);` ? And thanks for the help. :D – C.topo Apr 28 '17 at 06:16
  • I have edited my post and testing your code. I hope you don't mind to check and correct the mistake I did. Thanks! @Stephen Lightcap – C.topo Apr 28 '17 at 07:00
  • @C.topo check out the edit, it's working on my machine as is, just replace the file with your audio file – Stephen Apr 28 '17 at 07:42
  • I have followed and even copy paste all your code. But the pause button did nothing. Nothing error being shown also, weird. Btw I really appreciate your help and Thanks again! I'll try to fix this and let you know the result later. :D @Stephen Lightcap – C.topo Apr 28 '17 at 08:47
  • Is there a way I can send you the code that Im working on? I mean the html file of it. Maybe in that way you can try to run it on your pc. Because I did try it on mozilla and chrome and the pause are still not working. @Stephen Lightcap – C.topo Apr 28 '17 at 09:42
  • Here the link to view it @Stephen Lightcap. https://drive.google.com/open?id=0B7_v0AE5KCO7SFc0d1B4dnRYNkU – C.topo Apr 28 '17 at 10:10
  • @C.topo Yeah it wasn't working until I used the 0.6.2 version over 0.5.2 - `` made it work – Stephen Apr 28 '17 at 13:14
  • Thanks! It finally work. Sorry a bit busy and dont have time to online until now. You a lifesaver. :D @Stephen Lightcap – C.topo May 02 '17 at 01:32