How to mute/unmute multiple audio objects on different slides with one button

Problem:
Is there a way to create an Execute javascript action for a button that On Click would mute and then On Click would unmute a list of many audio objects that are on different slides that would stay muted until the button is clicked again to change it (so it doesn’t reset after every slide change)? Or something similar that has the same function.

ActivePresenter version: 9.3.0

OS: Windows 11

Hi,

Yes, it is possible to achieve that in ActivePresenter.
You can add an Execute JavaScript action to the project On Load event. (ActivePresenter button > Project > Properties > Properties pane > Interactivity tab) then enter the following scripts:

if (typeof window.isMuted === 'undefined') {
    window.isMuted = false;
}

window.toggleMuteAll = function () {
    window.isMuted = !window.isMuted;

    const audios = document.getElementsByTagName('audio');
    for (let audio of audios) {
        audio.muted = window.isMuted;
        if (window.isMuted) {
            audio.pause();
        } else {
            try {
                audio.play();
            } catch (e) {
                console.warn('Autoplay blocked', e);
            }
        }
    }
};

window.handleSlideLoad = function () {
    const audios = document.getElementsByTagName('audio');
    for (let audio of audios) {
        audio.muted = window.isMuted;
        if (!window.isMuted) {
            try {
                audio.play();
            } catch (e) {
                console.warn('Autoplay blocked', e);
            }
        }
    }
};

const observer = new MutationObserver(() => {
    window.handleSlideLoad();
});
observer.observe(document.body, { childList: true, subtree: true });

Next, add an Execute JavaScript action to the button’s On Click event.
Then, use the following script:

// Call the global function defined at project load
window.toggleMuteAll();

Note:

  • The button needs to show over multiple slides (right-click the button > Show over Multiple Slides)
  • Name the audio tracks the same (For example, audio) and deselect their Autoplay option. (Properties pane > Media tab > Playback Options)

Hope that it helps.
BR,
Thuy

1 Like

Thanks for replying Thuy!:slightly_smiling_face: Sorry I forgot to mention that I just need the button to appear on one slide if possible. I would like the button to mute a specific list of audio tracks (with different names) not all audio tracks. I’m new to using javascript on projects so I’m not sure if this is possible to do on ActivePresenter.
Many thanks

Hi,

Kindly check the attached project to see if it addresses your concern.

Then, open the project On Load event > Execute JavaScript action (ActivePresenter button > Project > Properties > Properties pane > Interactivity tab).
To mute specific audio tracks, you can enter their name in the first line of the script.

BR,

1 Like

This works perfectly!:slightly_smiling_face: Thank you for your time and detailed support - it’s truly appreciated.

1 Like