Skip to content

Instantly share code, notes, and snippets.

@SeePetulaCode
Created February 17, 2021 06:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SeePetulaCode/bd3fb3b21eb58d5eeb52d66166f53a70 to your computer and use it in GitHub Desktop.
Save SeePetulaCode/bd3fb3b21eb58d5eeb52d66166f53a70 to your computer and use it in GitHub Desktop.
Click Image, Play Sound
<audio id="myAudio" onended="audioEnds()">
<source src="https://res.cloudinary.com/b1917/video/upload/v1613544387/codepen001/kid_laugh.mp3" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
<div id='playbutton' onclick="thisPlayer()" type="button">
<img src='https://assets.codepen.io/1312923/Play_dark+backg%402x.png?format=auto' id='image'>
</div>
let x = document.getElementById("myAudio");
let imgChange = document.getElementById("image");
function playAudio() {
x.play();
imgChange.src = "https://assets.codepen.io/1312923/Pause_dark+backg%402x.png";
}
function pauseAudio() {
x.pause();
x.currentTime = 0;
imgChange.src = "https://assets.codepen.io/1312923/Play_dark+backg%402x.png";
}
function thisPlayer() {
x.play();
imgChange.src = "https://assets.codepen.io/1312923/Pause_dark+backg%402x.png";
}
function audioEnds() {
imgChange.src = "https://assets.codepen.io/1312923/Play_dark+backg%402x.png";
}
body{
background-color: #ae638e;
}
#playbutton {
margin: 10% 50%;
}
img {
width: 200px;
margin: auto;
}
img:hover {
cursor: pointer;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment