Instantly toggling off audio through the settings API

Another question! (I’m using the latest version of Sugarcube.)

How would I go about letting the player toggle all audio off through the settings API? I understand the overall commands (like mute) in the audio macro documentation, as well as how to make music in a passage play conditionally according to the settings:

<<if is true>><<playlist "menusongs" loop play>><</if>>

I’m just not sure how to make all sound stop playing instantly when the player toggles the audio setting on or off in the settings menu. Right now, it only works if I toggle the setting off, and then refresh the page, allowing the condition to be evaluated (so the music doesn’t play if the toggle is now switched to “off” or “false”). But is it possible to attach the mute command to the settings toggle so that the change occurs immediately?

1 Like

I’m new at working with Sugarcube and audio so this is just a summer day onlooker pointing at the car’s open hood suggestion, but would it work if you put the kill-audio macro in a dialog popup? I’m not sure if that counts as a new page or a refresh. Instead of a switch in the menu it would be a link like “Mute/Restore Audio” that pops a dialog that says “Audio is now off/on” and runs the mute audio macros.

Or if necessary, that dialog can be a confirmation “Do you want to mute audio?” with a link that refreshes the page as you need.

The Audio related macros used the SimpleAudio API internally to achieve their desired result. And that API has a SimpleAudio.stop() function that can be used to stop all playing tracks.

If you look at the 2nd of the Setting.addToggle() function’s examples (that covers “widescreen”) you will see that you can associate a call-back function with the setting, which gets called when the value of the setting is initialised or changed.

You could use a similar call-back function with your own music setting to stop all playing tracks when the setting equals false. The code to place in your project’s Story JavaScript area would look something like the following…

// Setting up a toggle control for the settings property 'music' w/ callbacks
var settingMusic = function () {
	if (! { // is false

Setting.addToggle("music", {
	label    : "Play Music?",
	default  : true,
	onInit   : settingMusic,
	onChange : settingMusic

Thank you so much, that worked perfectly! I wasn’t aware of the SimpleAudio API’s connection, but that’s great to know :slight_smile: