Selective autoplay of audio for certain notes

I’ve been looking for something like this myself, but unfortunately, there doesn’t seem to be a good way to interrupt audio from a card code. Unlike images, for example, audio files do not get embedded into card’s HTML, and the playback is handled by Anki in the background.


There are two more workarounds, each with its own limitations, that I know of.

One is to disable all autoplay in deck settings, and use JS to automatically “click” the needed buttons. (you can surround the respective audio references with <autoplay> tag, for example, and modify all selectors adding this tag, e.g. “autoplay > .soundLink”, to target the selected audios).

Actually, disabling autoplay is not even necessary if every card will have at least one <autoplay> button, because activating that button with JS will prevent the rest of the buttons from being played back by Anki. This behavior is exploited in the other method, which uses playing an empty audio file to disrupt the default playback. This will work even when no autoplay files are present on a card, but has a downside of having to keep an extra field referencing the empty file on each note (maybe it can be replaced with some static HTML, I’m not sure).

But the main issue with both methods arises when there are multiple files on a card that need to be auto-played. “Clicking” on all of them at once with JS will only playback the last one because each consecutive click will interrupt the previously started audio, so some delay has to be added between clicks. Since JS has no access to audio files and does not receive any feedback from Anki to know when an initiated playback has finished, the only apparent solution is to add a static delay, which, if the audiofiles have significantly different lengths, will inevitably cut the longer files off, and add redundant pauses after each short audio.