0

Despite my readings I'm still limited in javascipt / soundJS syntax with using flash html5 canvas. I would like to pause and resume my sound with mouseover and mouseout events on the same button. I mean:

  • You hover the button: the music starts.
  • You mouseout: the music pauses.
  • You hover it again: it resumes from the position it has been paused with precedence.

According to the SoundInstance Class doc, you can do that with myInstance.pause(); and myInstance.resume(); methods. And I suppose "var sound;" behaves likes "myInstance" since sound.pause(); works.

With the js code below I can pause the sound and resume my sound, but not on the same button, I need a button_2 to do that.

I think I'm missing a condition in mouseover function to "check if the sound is paused or not and then resume it or start playing.

var sound;
function fl_MouseOverHandler(){  
    sound = playSound("monstres"); 
}  
this.mybutton.addEventListener("mouseover", fl_MouseOverHandler);  


function fl_MouseOutHandler()  { 
    sound.pause();  
} 
this.mybutton.addEventListener("mouseout", fl_MouseOutHandler);  

function fl_MouseOverHandler_2(){  
    sound.resume();
}  
this.mybutton_2.addEventListener("mouseover", fl_MouseOverHandler_2);

Any hint or link is welcome. Thank you.

2 Answers2

0

what about something like this?

var isPlaying = false;

function togglePlayback(){
    if(isPlaying){
        isPlaying = false;
        sound.pause();
    } else {
        isPlaying = true;
        sound.resume();
    }
}

this.some_button.addEventListener("some_event", togglePlayback );
Nick Briz
  • 1,917
  • 3
  • 20
  • 34
0

You are an angel sent to earth. Meanwhile I've found dome docs on javascript and managed to code my beloved function with something in the same spirit using a boolean variable. Here's my code:

var playing = new Boolean(false);

function fl_MouseOverHandler() {
    if (playing == false) {
        exportRoot.soundInstance = playSound("monstres");
        playing = true;
        exportRoot.soundInstance.on("complete", handleComplete);
        function handleLoop(event) {
            exportRoot.soundInstance = playSound("monstres");
        }
    } else {
        exportRoot.soundInstance.resume();
        }
    }
this.bout_zone_son.addEventListener("mouseover", fl_MouseOverHandler);

function fl_MouseOutHandler() {
    exportRoot.soundInstance.pause();
    }

this.bout_zone_son.addEventListener("mouseout", fl_MouseOutHandler);

I can't use if (playing) {....} syntax because:

  • i must first fire the sound with my button and then check if it's palying or not to resume it or start it.
  • I think I can't type sound.pause without first instanciating "sound = playSound("mylinkedsoundfromlibrary"); or my case exportRoot.soundInstance = playSound("monstres");

Well, thanx anyway for giving a shot in the right direction.

  • In order to use the `playSound` method (that Flash generates) as you have shown, you have to modify it. I believe the existing function just calls `createjs.Sound.play(...)`, and does not return the instance that is generated. It should instead `return createjs.Sound.play(...);`. – Lanny Dec 09 '15 at 17:37