Skip to content
Advertisement

How can I pause or stop the audio in this working script? [closed]

This is a working script for start to play the desired audio. Could anybody tell good a good solution for pause or stop the audio by clicking on the onclick paragraph again?

<!DOCTYPE html>
<html lang="hu">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=script, initial-scale=sript">
<title>English Homonyms</title>
<link rel="stylesheet" href="css/style.css">
    
<script type="text/javascript">

function play(param) {

const playlist = [
"0 => nincs",
"audio/audio_01.mp3",
"audio/audio_02.mp3",
"audio/audio_03.mp3"
];

var list = param;
var music = playlist[param];

var audio = new Audio(music);
audio.play();
}

</script>

</head>
<body>

<h1>ENGLISH PRONUNCIATON NONSENSES – HOMONYMS</h1>

<p onclick = "play(1)">1. The bandage was wound around the wound.</p>
<p onclick = "play(2)">2. The farm was used to produce produce.</p>
<p onclick = "play(3)">3. The dump was so full that it had to refuse more refuse.</p>

</body>
</html>

Answer

let nowPlaying = 0
let nowAudio = new Audio()

function play(param) {
    const playlist = [
        "0 => nincs",
        "audio/audio_01.mp3",
        "audio/audio_02.mp3",
        "audio/audio_03.mp3",
    ];

    var lista = param;
    var zene = playlist[param];

    if (nowPlaying === param) {
        if (nowAudio.paused) {
            nowAudio.play()
        } else {
            nowAudio.pause()
        }
    } else {
        nowAudio = new Audio(zene);
        nowAudio.play();

        nowPlaying = param
    }
}
Advertisement