Play audio and restart it onclick

To just restart the song, you’d do:

function play() {
    var audio = document.getElementById('audio1');
    if (audio.paused) {
        audio.play();
    }else{
        audio.currentTime = 0
    }
}

FIDDLE

To toggle it, as in the audio stops when clicking again, and when click another time it restarts from the beginning, you’d do something more like :

function play() {
    var audio = document.getElementById('audio1');
    if (audio.paused) {
        audio.play();
    }else{
        audio.pause();
        audio.currentTime = 0
    }
}

FIDDLE

Leave a Comment