JSFiddle - React, Tailwind, and code Playground

by Sergio Sánchez

HTML

<button onclick="AudioPlay('http://sprint.outcodingtest.com/Content/Audios/req/Answer-214.mp3');"> Boton </button>

JavaScript

$(document).on("audioFinishedAnswer", answerClick);
  var audio = new Audio('');
function SimpleAudioPlay(file) {
    if (audio.currentTime != 0) {
        audio.pause();
        audio.currentTime = 0;
    }
    audio = new Audio(file);
    audio.play();
};
/*In html means if its in the actual html and not a javascript call*/
function AudioPlay(file) {
    
    if (file != null && file != "" && typeof file != 'undefined' && file.indexOf('.mp3') > 0) {
        if (audio.currentTime != 0) {
            audio.pause();
            audio.currentTime = 0;
        }
        audio = new Audio(file);
        audio.play();
        audio.addEventListener("ended", TriggerEventAudioFinishedAnswer);
    }
}

function TriggerEventAudioFinishedAnswer() {
    $.event.trigger({
        type: "audioFinishedAnswer"
    });
}

function answerClick() {
    SimpleAudioPlay('http://sprint.outcodingtest.com/Content/Audios/answers/Answer215.mp3');
}