Is there a way to have an audio component play only once?

I would like for my audio component to play once and not restart. I don’t see a setting in the component that allows me to choose that option. Is there another way to achieve this?

Hi @warrengentry,

I’ve checked into this for you.

The audio itself is fine, but the Audio component is currently treating the end of the track as a signal to play the next track. Because there is only one audio file in the component, it ends up starting that same track again, which is why it keeps looping.

For now, you can copy some custom code into the page in Elements that tells the audio player to stop once that track reaches the end. This doesn’t change the audio file itself and only affects this particular track.

<script>
document.addEventListener('ended', function (event) {
    if (
        event.target instanceof HTMLAudioElement &&
        event.target.src.endsWith('/audio-4-tradesmen.mp3')
    ) {
        event.stopImmediatePropagation();
    }
}, true);
</script>

The audio should now play through once and then stop normally.

Republish your site once you get that code added and give it a try. Let us know how you get on.