Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>How to Build a Custom Audio Player</title>
- <style>
- .clone {
- position: absolute;
- top: 10px;
- left: 10px;
- text-decoration: none;
- background-color: red;
- width: 150px;
- text-align: center;
- color: snow;
- font-family: arial black;
- padding: 5px 20px;
- }
- body {
- margin: 0;
- background-color: ivory;
- }
- #player_container {
- width: 1000px;
- height: 50px;
- position: absolute;
- top: 50px;
- left: 200px;
- border: 2px solid red;
- border-radius: 10px;
- background-color: white;
- }
- #time_control {
- background-color: transparent;
- position: absolute;
- top: 0;
- left: 0px;
- display: inline-block;
- width: 190px;
- height: 100%;
- box-sizing: border-box;
- display: flex;
- justify-content: flex-end;
- align-items: center;
- }
- #bt1 {
- width: 60px;
- text-align: right;
- height: 25px;
- border: none;
- outline: none;
- float: right;
- margin: 0 2px;
- font-size: 20px;
- font-weight: 700;
- display: flex;
- justify-content: flex-end;
- align-items: center;
- }
- #bt2 {
- width: 60px;
- text-align: right;
- height: 25px;
- border: none;
- outline: none;
- float: right;
- margin: 0 2px;
- font-size: 20px;
- font-weight: 700;
- display: flex;
- justify-content: flex-end;
- align-items: center;
- }
- #play,
- #pause {
- position: absolute;
- border: none;
- outline: none;
- transform: translate(5px, 5px);
- }
- #icon_play,
- #icon_pause {
- width: 35px;
- }
- #mute,
- #unmute {
- position: absolute;
- border: none;
- outline: none;
- right: 0;
- transform: translate(-5px, 5px);
- }
- #icon_mute,
- #icon_unmute {
- width: 35px;
- }
- #h1 {
- display: block;
- text-align: center;
- font-size: 30px;
- font-weight: 900;
- margin: 50px 0;
- font-family: courier new;
- }
- #range_container {
- width: 680px;
- border: 5px solid;
- position: absolute;
- left: 220px;
- top: 20px;
- background-color: black;
- cursor: pointer;
- }
- #range {
- width: 20px;
- height: 20px;
- background-color: red;
- transform: translate(-10px, -10px);
- display: inline-block;
- border-radius: 50%;
- cursor: pointer;
- position: absolute;
- }
- </style>
- </head>
- <body>
- <div id='player_container'>
- <audio id='audio'>
- <source src='https://1xpto.netlify.app/Audio/audio1.mp3' type='audio/mp3'>
- </audio>
- <div id='time_control'>
- <button id='bt1'>0</button>
- <button id='bt2'>0</button>
- </div>
- <button onclick='Pause()' id='pause'><img id='icon_pause' src='https://1xpto.netlify.app/Icons/stop.png'></button>
- <button onclick='Play()' id='play'><img id='icon_play' src='https://1xpto.netlify.app/Icons/unstop.png'></button>
- <button onclick='Mute()' id='mute'><img id='icon_mute' src='https://1xpto.netlify.app/Icons/mute.png'></button>
- <button onclick='playSound()' id='unmute'><img id='icon_unmute' src='https://1xpto.netlify.app/Icons/unmute.png'></button>
- <div id='range_container'>
- <span id='range'></span>
- </div>
- </div>
- <a class='clone' title="Subscribe and hit the notification bell for more updates. It's free." href='https://www.youtube.com/channel/UCqLpDK0eOsG1eEeF9jOUZkw'>SUBSCRIBE</a>
- <script>
- audio.onloadedmetadata = function() {
- foo = getComputedStyle(range_container).getPropertyValue("width");
- var x = parseInt(foo);
- length = x / audio.duration;
- };
- audio.ontimeupdate = function() {
- range.style.left = audio.currentTime * length + "px";
- bt1.innerHTML = audio.currentTime.toFixed(0);
- timer = bt2.innerHTML = audio.duration.toFixed(0);
- bam = timer - audio.currentTime + 1;
- bam--;
- bt2.innerHTML = bam.toFixed(0);
- };
- range_container.onmousedown = (event) => {
- x = event.clientX - 425;
- audio.currentTime = x / length;
- range.style.left = audio.currentTime * length + "px";
- };
- onmousedown = (event) => {
- range_container.onmousemove = function(event) {
- x = event.clientX - 425;
- audio.currentTime = x / length;
- range.style.left = audio.currentTime * length + "px";
- };
- };
- onmouseup = function() {
- range_container.onmousemove = function() {
- range.style.left = x;
- };
- };
- play.onclick = () => {
- if (audio.paused) {
- audio.play();
- play.style.display = "none";
- }
- };
- pause.onclick = () => {
- audio.pause();
- play.style.display = "block";
- };
- unmute.onclick = () => {
- audio.muted = true;
- unmute.style.display = "none";
- };
- mute.onclick = () => {
- audio.muted = false;
- unmute.style.display = "block";
- };
- audio.onended = () => {
- audio.play();
- };
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement