How to play an audio several times in a loop?

Question:
How to auto play audio in a loop?

Product:
Wix Studio, Wix Audio element and Velo

What are you trying to achieve:
I have certain audio files on my site, which when played should repeat with a 1 second delay in between until user pauses or goes to the pre/next audio. This audio is a short lesson to teach certain language (and repetitions help my users).

What have you already tried:

  1. I have an Wix audio element called #audioPlayer3
  2. User presses play
  3. After the first play ends, it fires a OnEnded event
  4. In OnEnded event, I fire another $w.(“#audioPlayer3”).play();
    Audio does not replay.

Additional information:
This audio is inside a repeater
Each item in the repeater is loaded from a collection
Each item consists of certain display text and an audio file (mp3).
It works ok for the first item in the repeater. When I navigate to the next items (by the prev/next buttons, it plays only once).

Any help to achieve this is appreciated.

My live site:

Browser Chrome/Brave
Device: Laptop or iPhone

My code snippet:

export function audioPlayer3_ended(event) {
	// This function was added from the Properties & Events panel. To learn more, visit http://wix.to/UcBnC-4
	// Add your code for this event here: 
    //console.log("Audio ended. Asking to play again", "curRep = ", curRep);
    $w('#audioPlayer3').play();
    return;
}

Try to get this to work…

let playCount = 0;
const maxPlays = 3;

// Connect the event handler to the 'onEnded' event in Wix Editor
$w.onReady(()=> {
    $w('#audioPlayer3').onEnded(restart);
});

function restart(event) {
    // Increment the play count
    playCount += 1;

    // Check if the play count is less than the maximum allowed plays
    if (playCount < maxPlays) {
        // Play the audio again
        $w('#audioPlayer3').play();
    } else {
        // Log that the audio has played the maximum number of times and reset the play count
        console.log("Audio played 3 times, stopping now.");
        playCount = 0; // Optional: reset if you want to play 3 times again in the future
    }
}