How to get several audio files in one field to play randomly?

Hi, I tried the “Randomizing card content” following your instructions but this didn’t randomize the sound for me. I still had two audio file buttons show up for each “[sound:…]” file.

I worked on the script below with AI and I can now get a file to play randomly, though it only works when the automatic play is turned off. Is there a way to get around this? I have a script that plays a file randomly automatically when loading up a card but likewise the setting in Anki must be turned off. Randomly playing an audio file shouldn’t be this difficult :zany_face:

{{Audio}}
<script>
(function() {
    setTimeout(() => {
        // Get all audio buttons that Anki created
        let audioButtons = document.querySelectorAll('.replaybutton, .soundLink');
        
        if (audioButtons.length > 1) {
            // Pick random index
            let randomIndex = Math.floor(Math.random() * audioButtons.length);
            
            // Store original onclick handlers
            let originalClicks = [];
            audioButtons.forEach((btn, i) => {
                originalClicks[i] = btn.onclick;
            });
            
            // Hide all original buttons
            audioButtons.forEach(btn => {
                btn.style.display = 'none';
                btn.style.visibility = 'hidden';
            });
            
            // Create SINGLE new button
            let newButton = document.createElement('a');
            newButton.className = 'soundLink';
            newButton.href = '#';
            newButton.textContent = '🔊';
            newButton.style.display = 'inline-block';
            
            // Function to play our random audio
            const playRandomAudio = function(e) {
                if (e) {
                    e.preventDefault();
                    e.stopPropagation();
                }
                
                // Stop any currently playing audio
                let allAudio = document.querySelectorAll('audio');
                allAudio.forEach(audio => {
                    audio.pause();
                    audio.currentTime = 0;
                });
                
                // Execute the original click handler for our random audio
                if (originalClicks[randomIndex]) {
                    originalClicks[randomIndex].call(audioButtons[randomIndex], e || new Event('click'));
                }
            };
            
            // Add click handler to our button
            newButton.onclick = playRandomAudio;
            
            // Replace container with our new single button
            let container = document.getElementById('audio-container');
            container.innerHTML = '';
            container.appendChild(newButton);
            
            // Override Anki's 'r' key behavior - ALWAYS set this up
            document.addEventListener('keydown', function(e) {
                if (e.code === 'KeyR' && !e.ctrlKey && !e.altKey && !e.metaKey) {
                    e.preventDefault();
                    e.stopPropagation();
                    playRandomAudio(e); // Use our custom playback function
                    return false;
                }
            }, true);
            
            // Optional: Auto-play (remove these 3 lines if you don't want it)
            // setTimeout(() => {
            //     playRandomAudio();
            // }, 100);
        }
    }, 150);
})();
</script>