Automatically play audio of specified fields

Hello,
I have a card on which’s backside are multiple fields that contain audios. I have enabled the option to not play the audio automatically in the deck options. So normally audios are not played automatically. I only want certain of these audios to be played automatically. This should be done through a script in the card templare. I found the following code online which enables this. BUT the problem is that only the first audio with the id=“autoplay” is being played automatically, NOT the second audio. I have tried a lot and chatted back and forth with ChatGpt to find a solution but nothing has worked so far.

----THESE ARE THE AUDIO FILES

---- DON’T GET CONFUSED HERE: THE AUDIOS WITH THE CLASS “autoplay” ALSO HAVE THE CLASS “dontshow”, WHICHS PURPOSE IT IS TO HIDE THE AUDIO BUTTONS FROM THE CARD. (SINCE THEY’RE PLAYED AUTOMATICALLY ANYWAY THERE IS NO NEED FOR AN AUDIO BUTTON).

<span class="dontshow autoplay">{{Audio German}}</span>
<span class="dontshow autoplay">{{Audio French}}</span>
{{Audio Chinese}}

THIS IS THE SCRIPT WHICH IS LOCATED IN THE BACK TEMPLATE AS WELL

var elem = document.querySelector(".autoplay .soundLink, .autoplay .replaybutton");
 	if (elem) {
 		elem.click();
 	}

As already stated: this approach works, but only the first audio is played automatically (Audio German). I would want the first audio to be played automatically (Audio German) and the second as well (Audio French).

This is an option I’ve tried, that hasn’t worked:

var elements = document.querySelectorAll(".autoplay .soundLink, .autoplay .replaybutton");

if (elements.length > 0) {
    elements.forEach(function(elem) {
        elem.click();
    });
}

I would truly appreciate any help. Thank you!

In your second codes, several elem.click() are called at the same time, thus I think they will play at the same time.
You may add a delay between each audio you want to play, or use the audio ended event like:

https://forums.ankiweb.net/t/how-to-play-audio-continuously-using-src/29136/2

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.