1

I've used this script to make it possible to turn on and off my audio. However, as I don't have an option to click and loop my song on my timeline. When this song ends, it doesn't play again.

I would like to know if I can add something to this script to make my song loop after it ends.

I've found this and have tried ...

createjs.Sound.play("soundId", {loop:-1});

But it doesn't work!

I'm working on html5 canvas on Adobe Animate!

I'm using this script, it works:

createjs.Sound.on("fileload", function handleLoad(event) {
    root.audio = createjs.Sound.play("soundID");
});

createjs.Sound.registerSound("sounds/myaudio.mp3", "soundID");  

//this.audio1.paused = true; // pause     
//this.audio1.paused = false; // resume 

this.onOff_btn.on("click", function(){
    if(root.audio.paused) {
        root.audio.paused = false; // resume
    } else {
        root.audio.paused = true; // pause
    }
});

Thank you in advance!

Sara Silva
  • 85
  • 2
  • 8
  • what is `createjs` – Jaromanda X Dec 01 '17 at 00:06
  • I must clarify I'm a beginner and this code was told by my current teacher as a way to call the audio from a folder and play it if I click on and pause it If I click on off (my onOff_btn); but as I've learned, I think createjs is a javascript library online that allows me to get some resources? But now, because of your question, I really would like to know If I'm getting it wrong! – Sara Silva Dec 01 '17 at 00:15
  • I don't know - I just asked what `createjs` is, as it isn't anything that's part of the javascript language – Jaromanda X Dec 01 '17 at 00:18
  • @JaromandaX [`createjs`](https://createjs.com/) is a JavaScript library for working with the `HTML5` `canvas` – Hayko Koryun Dec 01 '17 at 10:47
  • Oh, ok @HaykoKoryun – Jaromanda X Dec 01 '17 at 10:57
  • the above comment lead me to [read the documentaion](https://createjs.com/docs/soundjs/classes/PlayPropsConfig.html) ... `var props = new createjs.PlayPropsConfig().set({loop: -1})` then you'd `createjs.Sound.play("soundId", props);` - took about 30 seconds of reading the documentation to find what you did wrong – Jaromanda X Dec 01 '17 at 10:57
  • Thank you for that information but I've already tried that, as I've read that link before, but the music doesn't play at all when I add that. But thank you anyway! I'll work until I find a solution! – Sara Silva Dec 01 '17 at 15:27
  • Use like this: createjs.Sound.play(id, createjs.Sound.INTERRUPT_EARLY, 0, 0, loop); and set loop in desire number, e.g. 9999 – gMirian Dec 01 '17 at 16:49

1 Answers1

2

If anyone is wondering how to do this, I found a way to make it work!

Here it is:

createjs.Sound.on("fileload", handleLoad);
createjs.Sound.registerSound("sounds/youraudio.mp3", "myID", 3);
 function handleLoad(event) {
 createjs.Sound.play("myID");
     // store off AbstractSoundInstance for controlling
     var myInstance = createjs.Sound.play("myID", {interrupt: createjs.Sound.INTERRUPT_ANY, loop:-1});
 }

 //this.audio1.paused = true; // pause     
//this.audio1.paused = false; // resume 

this.onOff_btn.on("click", function (evt) {
        v = createjs.Sound.getVolume();
        if (v < 0.5) {
            createjs.Sound.setVolume(1);
        } else {
            createjs.Sound.setVolume(0);
        }
    });

It loads your audio, loops it and has a button on which you click to make it play or pause! Thanks to everyone who have tried to help!

Sara Silva
  • 85
  • 2
  • 8
  • If you want to toggle volume, you can also use scope `myInstance` outside your `handleLoad` method, and toggle pause or muted. `myInstance.muted = !myInstance.muted; myInstance.paused = !myInstance.paused` – Lanny Dec 15 '17 at 18:06