Click on texts to play MULTIPLE audio

Hi there!

This script works fine!

forums.ankiweb.net/t/solution-for-click-text-to-play-sound/29436

But it only plays the first audio: [sound:one.mp3][sound:two.mp3].

How can I change this script to play all the audio in a row? Just like the Anki program itself does when opening a card.

You can’t without a workaround (like assuming all audio files are 3 seconds).

You can press r or, if you’re on mobile, use gestures to replay media.

Yes, it is. But it is necessary to click on the text.

The best you can do is merge the audio files in an external application.

If I don’t find a solution with the script…

JavaScript alone can’t solve this because audioplayback is handled by Anki on the back end, outside of reach for card scripts. They do not have access to the ongoing playback state or information about the duration of audio files. There is no API for requesting a playback of several files or catching a playback finish event either.

The potential workarounds, in addition to the already mentioned one, would be:

  1. Write an add-on that will implement the required API for card scripts to use (will take a lot of effort and won’t work on mobile)
  2. Store information about audio files’ durations in Anki fields explicitly (can be facilitated by a much simpler add-on, which won’t need to run constantly, so mobile remains an option)
  3. Use <audio> tags instead of [sound:] when inserting audio into fields (will make audio playback fully accessible to JS scripts, but will mess with Anki’s tracking of unused files)
  4. Use the AnkiWeb site for reviews instead of the app (it inserts all [sound:] as <audio> tags anyway and does not ruin unused files tracking, but will require adapting card templates to work in the web version)
2 Likes

Wow! Thank you very much for such interesting information. Something to think about…

1 Like

This is a tricky problem. I needed to write a class to be able to play from file lists, but I had to specify their names.
Class start:

    window.audioSWebManager = {        
        audio: null, // Current player
        playlist: [], // Play queue
        currentIndex: 0,
        playlistId: 0, // Playlist ID for tracking relevance
        isPlaying: false,
        loop: false,
        shuffleN: 0, // after how many cycles will the mixture be mixed
        loopN: 0, // current cycle number
                
        initAudio() {

I needed the ability to set the playback speed of files and file blocks: words and translations.
I simply implemented the idea of ​​being able to play a file slowly at first, then another word, and then back to normal speed.
I also have the ability to view the most recent word-translation pairs and start automatic pronunciation if you’re having trouble remembering them. I use the code like this:

// Function for playing a list of files
function playAudioList(files, loop = false, shuffleN = 0) {    
    /* files - array of objects or strings: ['file1.mp3', 'file2.mp3']
    или
      [
        {filename: 'file1.mp3', speed: 1.0},
        {filename: ':1000', speed: 1.0}, // pause 1 second
        {filename: 'file2.mp3', speed: 1.5},
        {filename: ':0', speed: 1.0} // end of block file mark (for shuffling)
      ] 
    shuffleN - after how many cycles will the mixture be mixed    
    */

In general, this entire code will be open source. Although it’s ready, it’s not yet publicly available, and I still need to debug the issues.

For now, I’ve made a couple of decks from www.goethe-verlag.com, one in each direction, and I’ll probably have to finish some more, as the available ones contain phrases, but not words, and the language isn’t quite right. ankiweb.net/shared/decks?search=book2

There’s a lot of code, it’s complex, and not for ordinary people.
Of course, when you have a template, it’s easier to use it for your own words, and only if it works.
If there were code libraries, a player like this would be useful. I almost have a fully developed idea for a player with more functionality, but it’s huge, and I don’t see any point in implementing it here for words yet, but for phrases, I might have to.
So, if I need to play files in a certain sequence… and this template has that option, I do:

<span style="display: none">{{Audio_Learn}}</span>

It’s just a hidden field that plays at the beginning, and for the pause, I created separate files and uploaded them to a shared folder so I can use them in the design. So, I have both the standard and my own voiceover… which even plays automatically in ankiweb.net when the map is shown.