<!DOCTYPE html>
<
html
lang
=
"en"
>
<
head
>
<
title
>Single Audio Playback at a Time</
title
>
<
meta
charset
=
"UTF-8"
/>
<
meta
name
=
"viewport"
content
=
"width=device-width, initial-scale=1.0"
/>
<
style
>
h1,
h3 {
color: green;
text-align: center;
}
.audio-container {
margin: 20px 0;
text-align: center;
}
button {
padding: 10px 20px;
font-size: 16px;
background-color: crimson;
color: #fff;
border: none;
border-radius: 5px;
cursor: pointer;
}
.playing {
color: green;
font-weight: bold;
}
</
style
>
</
head
>
<
body
>
<
div
class
=
"container"
>
<
h1
>GeeksforGeeks</
h1
>
<
h3
>Play Single Audio Element at a Time</
h3
>
<
div
class
=
"audio-container"
>
<
audio
src
=
id
=
"audio1"
>
</
audio
>
<
button
onclick
=
"playAudio('audio1')"
>
Play Cantina Band
</
button
>
<
span
id
=
"playing1"
></
span
>
</
div
>
<
div
class
=
"audio-container"
>
<
audio
src
=
id
=
"audio2"
>
</
audio
>
<
button
onclick
=
"playAudio('audio2')"
>Play Star Wars</
button
>
<
span
id
=
"playing2"
></
span
>
</
div
>
</
div
>
<
script
>
let currentAudio = null;
function playAudio(audioId) {
let audioElement = document.getElementById(audioId);
let playingSpan = document.getElementById(`playing${audioId.slice(-1)}`);
if (currentAudio && currentAudio !== audioElement) {
currentAudio.pause();
playingSpan.innerHTML = '';
}
if (audioElement.paused) {
audioElement.play();
currentAudio = audioElement;
playingSpan.innerHTML = 'Playing';
} else {
audioElement.pause();
currentAudio = null;
playingSpan.innerHTML = '';
}
}
</
script
>
</
body
>
</
html
>