Skip to main content
Participant
December 12, 2022
Answered

HTML 5 - stop 1 sound but keep the other sounds playing

  • December 12, 2022
  • 1 reply
  • 445 views

Hi 🙂

I am trying to convert an old AS3 game I created ages ago to HTML5.

The original had a few buttons each playing a different voice and you could click each of them once to start and again to stop. Stopping one would not affect the others who were already playing. The fun was to try and get a nice harmonious tune. I want to do this in HTML. I am very new to this!

I tried using createjs.Sound.play("tuneID"); to click a sound on and then createjs.Sound.stop("tuneID") to click it off but the latter simply switches all the sounds off 😞

I need the tune to start from the beginning whenever it is switched on so originally I had the button move the play head inside a symbol to a frame with sound event-Start/loop and then, when clicked again, to a frame with a stop sync. But I realised those options are not available with HTML.

Is there a simple way to do this?
Can it be done with sounds imported to the library or do I need to call external sound files?
Is this doable?
Any advice will be greatly appreciated!
Thank you.

This topic has been closed for replies.
Correct answer kglad

use:

 

var tune = createjs.Sound.createInstance("tuneID");

// etc for your other sounds. 

 

 

// then to play:

tune.play();

// then to stop:  

tune.stop();

1 reply

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
December 12, 2022

use:

 

var tune = createjs.Sound.createInstance("tuneID");

// etc for your other sounds. 

 

 

// then to play:

tune.play();

// then to stop:  

tune.stop();

Participant
December 13, 2022

Thank you so much for the super fast answer.
Sadly the play works great but the stop doesn't.
I have used this by swaping the tuneID with the sound's linkage name inside the library, is that the problem?

When I use the play and stop commands one after the other like this-
var tune = createjs.Sound.createInstance("soundLinkageID");
tune.play();
tune.stop();

The music does not play so it is as if the command is recognised.
But if I put the tune.stop() inside an if-else statment, or even if I have the 'create' and 'play' on frame one and then 4 frames later I place a 'stop' it does not stop.

It's like if it is not right after then it doesn't know which tune I am talking about 😞

kglad
Community Expert
Community Expert
December 13, 2022

well wait.  you're missing something basic about js code in animate.

 

if you want a variable that's usable in another frame, use:

 

this.tune = etc

 

this.tune.play();

 

// then in another frame on the same timeline  this.tune.stop() will work.

 

if you used the code i showed and your if-statement is in the same frame, you made some other error: show your code.