Turn auto-play "on" by code

Hello!

Example sentence on the front page:

Tuve que {{c1::dispararle}} a mi caballo.

I do not want the sentence to be read out automatically
I have therefore deliberately switched off “auto-play”.
Why?
When autoplay is on, the missing word can be easily guessed.

Instruction on the back:
text-align: left’>{{cloze:text}}{{audio}}

Now the autoplay-symbol appears, but the audio-sample is not played automatically.

Question:
How do I have to complete the statement so that on the reverse side the sample set is now played automatically.

Important:
Before calling up a new card, “auto-play” should automatically be set to “off” again.

Additional question:
how can i put two, free auto-play symbols on with short-cuts so that they play?

Thank you, Peter El Salvador

There’s a hacky way of achieving this in javascript. If you have a tag on the reverse side, when the card flips that tag is automatically executed. You can write a function to play the audio, then call that function in the script. Here’s a stripped down example:

<span class="answer-text">
	<div id="word-audio" style="display: none;">{{Vocabulary-Audio}}</div>
	<button onclick="play_word_audio()">Word</button>

	<div id="sentence-audio" style="display: none;">{{Sentence-Audio}}</div>
	<button onclick="play_sentence_audio()">Sentence</button><br>
</span>

<script>
    function play_word_audio(){
        var audioDiv = document.getElementById('word-audio');
        var audio = audioDiv.getElementsByTagName("*");
        audio[0].click();
    }
	function play_sentence_audio(){
        var audioDiv = document.getElementById('sentence-audio');
        var audio = audioDiv.getElementsByTagName("*");
        audio[0].click();
    }
    play_word_audio()  // Automatically plays word's audio on flip
</script>

Create <div> for each of the audio tracks “Vocabulary-Audio” and the “Sentence-Audio” in this case. We set “display:none” on both to not clutter things, though this is optional. Below each we have a button bound to the corresponding javascript function, so they’ll play the audio when clicked. We also call the play_word_audio() function at the bottom, so that the word is automatically played when the card is clicked

The full code for the card above, which is quite a bit more cluttered, is available here

1 Like

Dear Voovs,

thanks for your rapid answer.
Give me a lot of time to try it, as I’ve no idea from programming.
With 75 + x years, it’s not that easy to start with Anki or learning codes in a programming language.
Nonetheless one question:
Can I copy your extended code directly into the pages?

Thanks, Peter El Salvador

Ah my bad. Feel free to copy it. In your case this may be even easier though

  1. Go into “Browse”
  2. Find and click on your deck in the left menu
  3. About half way down the screen there should be a bunch of buttons. Click on “Cards…”
  4. Near the top click on “Back Template”
  5. Copy/paste everything from <script> to </script> from above at the very bottom of the editor

Now guessing from what you provided above, the audio is stored in the {{audio}} field. If that’s the case, copy/paste this code anywhere above the <script> tag

<div id="word-audio" style="display: none;">{{audio}}</div>
<button onclick="play_word_audio()">Text</button>

Now if you try reviewing the card, there’ll be a button labeled Text, once you flip the card. You can click that to replay the audio. It should also play it automatically

If the audio is stored in a different field, say {{Sentence}} for example, you’d just place that in instead of {{audio}}. If you’re not sure, scroll through the fields and find one that has what looks like an audio file typed in. For me it looks like [sound:061bb20c57c1fce6de649fabd5120aa7.mp3]. Use the title of that field

1 Like

@peschi is your goal to only hear the audio when the answer is revealed? If so, why not just place the {{audio}} field reference on the back template, and remove it from the front template?

Dear dae,

thanks for your advice.
I sound far easier than using JavaScript

The front page looks at the moment like:

Backpage looks like:

If I erase the command:
{{cloze:Text}} on the front page
the question will not be shown anymore on the front page.

Please be aware:
I’m a beginner in Anki and I don’t have any clue of Anki syntax, neither programming in Java.
At the moment it is more a trail and error, which is not a good way.

Nonetheless, I’m very convinced of the Anki app, special it is a great improofment of the “Leitner” system, a great help in E-Learnig.

Peter El Salvador

The deck author has placed the sound reference in the same field as the text, which makes things more complicated. While not trivial, it is possible to split it up. Splitting a field into multiple fields - Frequently Asked Questions

Dear dae,
thank you very much for your advice.

To say it in advance:
Deck: "9000 Spanish sentences with native speaker"
is a real treasure trove.
It was the first deck I downloaded.
Certainly not a deck for beginners in Anki.

Unfortunately, I have spent a lot of time in the last few days adding fields, English and German translation, audio samples etc…

From a methodological / didactic point of view, the deck would have to be completely restructured, additional notes added, etc.
Unfortunately, I have no idea how to import existing records into a new deck:
Spanish sentence and, very important: associated audio sample must be exported / imported to the new deck.
The individual fields in the deck look at the moment like this:
(some fields added and modified by me)



Under normal circumstances, I would never pack so much information onto one page.
But adding more notes seems difficult, hardly possible or time-consuming with the original deck.
I would prefer to export the Spanish sentences including audio samples into a new deck and redesign the new deck.
But unfortunately, I have no idea how to proceed.

Regards, Peter El Salvador

Please don’t be angry if I don’t respond to your answer immediately.
My free time during the holidays is over,
I can’t spend too much time with Anki anymore.

Autoplay is intentionally set to “off”.

First page:
a Spanish sentence, a Spanish word / expression as a cloze.
The (cloze) text / expreccion has to be “learned” or repeated.
Therefore, Autoplay was intentionally set to “off”.

Second page:
It would be nice when calling up the second page (answer) now the Spanish sentence is played automatically - automatic autoplay: “on”.

Questions a:
What is the code to set autoplay to “on”.

Question b:
When a new record is called, logically autoplay should be set to autoplay: “off” again.

What is the code to set Autoplay back to “off” before calling the answer?

Thank you, Peter El Salvador

But what is the field contains text+Audio:

Example_fld = "I will test this [sound:google-ba798ff2-c464647f-0b570b93-e7a7fdd6-bc9a9528.mp3]

How to make a JavaScript to look like this:

:sound: I will test this

with the same result - when pressing button, it plays mp3 for this field ?