+3 votes
by (710 points)

I had used this script to toggle a on/off switch in the Settings.

var audioEnabledHandler = function () {
	if (!settings.audioEnabled) { // is false
		/*
			Stops all audio currently playing, via <<audio>> or <<playlist>>,
			when the setting is disabled.
		*/
		new Wikifier(null, "<<masteraudio stop>>");
	}
};
Setting.addToggle("audioEnabled", {
	label    : "Enable audio? - Activer l'audio?",
	default  : true,
	onInit   : audioEnabledHandler,
	onChange : audioEnabledHandler
});

Its pretty nice, but if I switch the audio off, I cannot toggle it back on in the current passage. It only plays again when another audio file comes up. I have seen another Twine story using this script and the audio could be switched on and off in the same passage without problems. So what did I do wrong? I also put these for my audio files on every passage (stop one file and play the next one).

<<nobr>><<if settings.audioEnabled>><<audio "DreamsBecomeReal" stop>><<audio "ClassicHorror3" loop play>><</if>><<endnobr>>

 

1 Answer

+3 votes
by (68.6k points)
selected by
 
Best answer

The setup you're currently using is meant to allow players to completely disable audio.  It is not meant to be a means of muting the audio.

With the above caveat in mind, you could reshow the current passage using SugarCube's built in Engine.show() method.  For example, change the setting to look like the following:

var audioEnabledOnInit = function () {
	if (!settings.audioEnabled) { // is false
		/*
			Stops all audio currently playing, via <<audio>> or <<playlist>>,
			when the setting is disabled.
		*/
		new Wikifier(null, "<<masteraudio stop>>");
	}
};
var audioEnabledOnChange = function () {
	/*
		We call the onInit handler here to avoid duplicating its code.
	*/
	audioEnabledOnInit();

	if (settings.audioEnabled) { // is true
		/*
			Reshows the current passage, thus allowing audio to begin playing
			again, when the setting is enabled.
		*/
		Engine.show();
	}
};
Setting.addToggle("audioEnabled", {
	label    : "Enable audio? - Activer l'audio?",
	default  : true,
	onInit   : audioEnabledOnInit,
	onChange : audioEnabledOnChange
});

That will, upon changing the setting to enable audio again, reshow the current passage, allowing any audio macro invocations within the passage to execute as normal.  NOTE: Any interactive elements within the passage will be reset as a result of reshowing the passage.
 

Additionally.  As either an alternative or a companion to the above setting, you could provide a button to toggle the mute state of the master audio.  For example, you could add something like the following to the StoryMenu special passage—it could also be turned into a setting if desired:

<<link "Toggle Audio">>
	<<if setup.muteAudio>>
		<<masteraudio unmute>>
		<<set setup.muteAudio to false>>
	<<else>>
		<<masteraudio mute>>
		<<set setup.muteAudio to true>>
	<</if>>
<</link>>

That will (un)mute the master audio.

 

by (710 points)

Wow, this is perfect! smiley

...