Turn off auto-play for hidden elements

Anki’s auto-play feature plays all audio files in a card, including the ones that have been hidden. Is it possible to have the auto-play feature only play the files that are not hidden?

This currently breaks the functionality of @AnkiQueen’s randomised templates or the current #1 Russian deck on AnkiWeb if you try to use auto-play on them. The latter’s author told me it’s also possible to provide an auto-play API of sorts from Anki’s side.

2 Likes

What do you mean exactly by hidden? Cloze? or with the html hidden attr?

No, not cloze. I’m talking about the latter.

Still confused as to what you’re asking for. As it stands the card renderer picks out av tags ([sound:...] etc) and autoplays them.

You can’t put a hidden or display:none on an av tag, so what is your addon adding exactly? An <audio> tag? Or applying hidden to the html block surrounding the av tag?

If it’s the latter, then the card renderer parser is going to have to parse html on top of the av tag syntax, which, and I could be mistaken, is highly unlikely to be added.

Your best bet might be to write an addon that uses the reviewer_will_play_question_sounds and beautifulsoup to parse the rendered html and see which tags are enclosed in a hidden html element.

I’m not using any add-ons. The note types I’m facing the problem in hide the fields in a <div> (in the template) and Anki’s auto-play feature still reads them out.

Unfortunately, I use AnkiDroid so that’s not feasible. If there’s a way to use JS to trigger a click after the audio file has loaded, I’d appreciate help.

Regardless, users need to turn off auto-play in the top Russian deck on AnkiWeb and so I wish this worked out of the box for everyone. I’m hoping dae can help.

Ah the old X-Y.. :sweat_smile:

There is (if i understood correctly)! A “playsound:…” url format in ankidroid

But i’m still very much not up to speed in that codebase, so I can’t provide much more detailed info than this breadcrumb rn

From what I see, it looks like it’s almost the same format as anki’s, i.e playsound:q:1 playsound:a:2 etc.

1 Like

would it be possible to provide an option to turn off autoplay for individual files? maybe by adding a class that anki checks before autoplaying a file? so if anything appears in a div with class no-autoplay anki skips them.

my understanding is some people use autoplay to play files but they hide the play button in their template. so simply disabling autoplay in hidden divs would affect those users.

perhaps there’s a better way to do this that im not aware of.

to restate the use-case, some templates are designed to show you different texts in the same card every time you study it (hiding other text). but currently audio elements can’t be hidden in the same way as text if you need auto-play.


edit: thought something like document.getElementById("hidden").innerHTML = " "; migt work but it doesn’t :sadge:

Will this help?


There is:

but it will only work for autoplaying a single audio file. There is no good way to queue several files, as far as I know.

I think not, the sound that should get muted keeps changing from one card to another.

I can give you an example of one of my note types:

<!-- this hides the text field for now --> 
 <div id="hidden">{{cloze:Text}}</div>

<!-- then we fill this target div with text using javascript -->
<div id="target"></div>

So, what you can do is have multiple text seperated by delimiters (I use ¦), the JS splits the text and randomly inserts one of the parts into the target div.

JS for front: (credit: AnkiQueen)

<script>
(()=>{
let clozes = document.getElementById("hidden").innerHTML.split("¦")
let rng = Math.floor(Math.random() * clozes.length)
localStorage.setItem("rng", rng)
document.getElementById("target").innerHTML = clozes[rng]
function close() {
}

})()
</script>

I really think we should get a native solution because these templates are quite popular and the top #1 russian deck also does something similar. It’s a shame we can’t use autoplay or TTS properly in these templates.

1 Like

This is something I really want too. I currently resort to turning off autoplay and using JavaScript to click a single audio button at random. I also display a large play button to easily replay that audio if I want to, since the replay feature plays all of the audio files (I’d usually use a swipe down gesture to replay the audio).

1 Like

This is also something I would want to do. I have some cards with 10 examples of audio or more, and perhaps even video (taken from Memrise’s database, btw). Of course, I don’t want 10 pieces of audio playing on every card. My current workaround is having just one audio playing when the card is displayed (random one in my case). I achieve this by putting all audio in a specific div, and then using

  var audioElements = document.querySelectorAll("#audio-container .soundLink, #audio-container .replaybutton"); // AnkiMobile & AnkiDroid / AnkiDesktop
	if (audioElements.length > 0) {
		var index = 0;
		audioElements[index].click();

to obtain the elements and click a random one, which will prevent all others from playing. Maybe this helps someone, but I’d also be glad about a real solution for this. I’m think this works without disabling autoplay and on all 3 platforms, but I’m not 100 % sure right now, especially about iOS.

AFAIK, if you have the field (e.g. {{Audio}}) in your anywhere in your template autoplay is going to play it. You can work around this by using the .click() you used on an empty audio file or using tts on an empty field/string.

This won’t work on AnkiDroid if you have any other scripts after the autoclicking one because of the bug I linked above. AnkiDroid uses href attribute instead of onclick, as desktop Anki does, so calling click() on the audio element prevents the rest of the card from loading. There is a workaround in the same bug report though, and there is an alternative implemented in this template (in case you are interested in Memrise stuff). At their core, both build on the same method you described, though.

In terms of working without disabling Anki autoplay, it does interrupt playing back of all the other files, but there is a small delay during which a fraction of the first audio may be heard, so disabling autoplay in Deck settings still seems preferable.