• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

New Here ,
Dec 12, 2022 Dec 12, 2022

Copy link to clipboard

Copied

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.

TOPICS
Code , How to

Views

326

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Dec 12, 2022 Dec 12, 2022

use:

 

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

// etc for your other sounds. 

 

 

// then to play:

tune.play();

// then to stop:  

tune.stop();

Votes

Translate

Translate
Community Expert ,
Dec 12, 2022 Dec 12, 2022

Copy link to clipboard

Copied

use:

 

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

// etc for your other sounds. 

 

 

// then to play:

tune.play();

// then to stop:  

tune.stop();

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 13, 2022 Dec 13, 2022

Copy link to clipboard

Copied

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 😞

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 13, 2022 Dec 13, 2022

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 13, 2022 Dec 13, 2022

Copy link to clipboard

Copied

Hi kglad,

Thank you!
So I am sure I got something simple wrong, and I think you are right and I did make some other error because-
I wanted to show you my code, but without all the mess (I was a little embarrassed at how messy it looked) so I just copied the most basic bits, replaced the long convoluted button names with simple generic ones and only coded for 2 buttons rather than the original 9 so I can send you a simple version to look at but when I tested that clean version - it worked!!! 🙂

I think i might have used capitals somewhere I shouldn't and possibly had some syntax problems.
But here is the clean code anyway just in case a future person wants to see it 🙂

But it all works now!
Thank you so much! you totally saved me! 🙂


Working coed:
this.stop();
var root = this;

// opening click just so stuff will work 🙂

this.clickMask.addEventListener("click",startF);
function startF () {
root.clickMask.visible=false;
root.gotoAndStop("game");
}
// Event listeners for all the buttons
this.btn1.addEventListener("click", musicF1)
this.btn2.addEventListener("click", musicF2)
// zero the counters
this.clickCount1=0;
this.clickCount2=0;
// stating the sounds like you said-
var tune1 = createjs.Sound.createInstance("snd1");
var tune2 = createjs.Sound.createInstance("snd2");
//singing functions
function musicF1(){
if (root.clickCount1===0) {
root.btn1.gotoAndStop("Csing")
root.clickCount1=1;
tune1.play();
} else {
root.btn1.gotoAndStop("Cstop")
root.clickCount1=0;
tune1.stop();
}
}
function musicF2(){
if (root.clickCount2===0) {
root.btn2.gotoAndStop("Csing")
root.clickCount2=1;
tune2.play();
} else {
root.btn2.gotoAndStop("Cstop")
root.clickCount2=0;
tune2.stop();
}
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 13, 2022 Dec 13, 2022

Copy link to clipboard

Copied

LATEST

you're welcome.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines