Skip to content

Instantly share code, notes, and snippets.

@brysonian
Created May 10, 2021 21:14
Show Gist options
  • Save brysonian/77ccad87560cc7fe1f7f55963d7bfe52 to your computer and use it in GitHub Desktop.
Save brysonian/77ccad87560cc7fe1f7f55963d7bfe52 to your computer and use it in GitHub Desktop.
easy audio player
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>TITLE</title>
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.14.0/css/fontawesome.min.css" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.14.0/css/solid.min.css" rel="stylesheet">
<style type="text/css">
#player {
display: none;
}
</style>
</head>
<body>
<div class="container">
<div class="controls">
<button id="playpause"><i class="fas fa-play"></i></button>
</div>
</div>
<audio class="player" id="player" preload="none">
<source src="{{PATH TO MP3}}" type="audio/mp3">
Your browser does not support the audio element.
</audio>
<script>
const player = document.getElementById('player');
const btn = document.getElementById('playpause');
const icon = btn.querySelector('i');
let isPlaying = false;
player.addEventListener('play', () => {
icon.classList.remove('fa-play');
icon.classList.add('fa-pause');
isPlaying = true;
});
player.addEventListener('pause', () => {
icon.classList.remove('fa-pause');
icon.classList.add('fa-play');
isPlaying = false;
});
btn.addEventListener('click', () => {
if (isPlaying) {
player.pause();
} else {
player.play();
}
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment