Forum Discussion

Re: Stop audio when javascript typewriting effect stops

While controlling the audio from SL with a variable trigger is easier, audio from JS is also possible. However, once you start manipulating audio through JavaScript, you may not be able to control it properly through SL triggers anymore. Best to use one or the other, not both.

For a JS only approach, try adding this at the begining of your script:

// Get the Storyline player object
var player = GetPlayer();

//Add these lines to target the audio, reset to begining, and play when typing script starts
const audio = document.querySelectorAll("audio")[0];
audio.currentTime = 0;
audio.play();

then, add this to the end of your script:

    } else {
        // When finished, update the Stoptypewriter variable in Storyline
        player.SetVar("Stoptypewriter", true);

        //Add this line to stop the audio when the typing is finished
        audio.pause();

    }
}

Note: This will only work if the user has interacted your slides first (e.g., button, text box, other slides, etc.)  JavaScript can't play audio directly before some kind of interaction has occured.

No RepliesBe the first to reply