If you add the following code to your JavaScript section:
// Check to see if trackID is the currently playing track
window.isPlaying = function (trackID) {
var track = SimpleAudio.tracks.get(trackID);
return track !== null && track.isPlaying();
};
or, if you're using an older version of SugarCube, prior to v2.28.0, use this JavaScript instead:
// Check to see if trackID is the currently playing track
window.isPlaying = function (trackID) {
var tracks = Macro.get("cacheaudio").tracks;
if (tracks.hasOwnProperty(trackID)) {
var currentTrack = tracks[trackID];
if (!currentTrack.audio.paused && currentTrack.audio.duration) {
return true;
}
}
return false;
};
Then you could do:
<<widget "ChangeMusic">>
<<if !isPlaying($args[0])>>
<<masteraudio stop>>
<<audio $args[0] play loop volume 0.25>>
<</if>>
<</widget>>
You can see other Twine/SugarCube audio sample code here, including a more advanced "PlayTrack" widget which you can use to make sure that the audio is cached as well (click "Jump to Start" in the UI bar to see a variety of other sample code there).
Hope that helps! :-)