Advertisement
1xptolevitico69

How to Build a Custom Audio Player

Jan 2nd, 2022
1,515
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5.36 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.  
  4. <head>
  5.   <meta charset="UTF-8">
  6.   <meta http-equiv="X-UA-Compatible" content="IE=edge">
  7.   <meta name="viewport" content="width=device-width, initial-scale=1.0">
  8.   <title>How to Build a Custom Audio Player</title>
  9.   <style>
  10.     .clone {
  11.       position: absolute;
  12.       top: 10px;
  13.       left: 10px;
  14.       text-decoration: none;
  15.       background-color: red;
  16.       width: 150px;
  17.       text-align: center;
  18.       color: snow;
  19.       font-family: arial black;
  20.       padding: 5px 20px;
  21.     }
  22.  
  23.     body {
  24.       margin: 0;
  25.       background-color: ivory;
  26.     }
  27.  
  28.     #player_container {
  29.       width: 1000px;
  30.       height: 50px;
  31.       position: absolute;
  32.       top: 50px;
  33.       left: 200px;
  34.       border: 2px solid red;
  35.       border-radius: 10px;
  36.       background-color: white;
  37.     }
  38.  
  39.     #time_control {
  40.       background-color: transparent;
  41.       position: absolute;
  42.       top: 0;
  43.       left: 0px;
  44.       display: inline-block;
  45.       width: 190px;
  46.       height: 100%;
  47.       box-sizing: border-box;
  48.       display: flex;
  49.       justify-content: flex-end;
  50.       align-items: center;
  51.     }
  52.  
  53.     #bt1 {
  54.       width: 60px;
  55.       text-align: right;
  56.       height: 25px;
  57.       border: none;
  58.       outline: none;
  59.       float: right;
  60.       margin: 0 2px;
  61.       font-size: 20px;
  62.       font-weight: 700;
  63.       display: flex;
  64.       justify-content: flex-end;
  65.       align-items: center;
  66.     }
  67.  
  68.     #bt2 {
  69.       width: 60px;
  70.       text-align: right;
  71.       height: 25px;
  72.       border: none;
  73.       outline: none;
  74.       float: right;
  75.       margin: 0 2px;
  76.       font-size: 20px;
  77.       font-weight: 700;
  78.       display: flex;
  79.       justify-content: flex-end;
  80.       align-items: center;
  81.     }
  82.  
  83.     #play,
  84.     #pause {
  85.       position: absolute;
  86.       border: none;
  87.       outline: none;
  88.       transform: translate(5px, 5px);
  89.     }
  90.  
  91.     #icon_play,
  92.     #icon_pause {
  93.       width: 35px;
  94.     }
  95.  
  96.     #mute,
  97.     #unmute {
  98.       position: absolute;
  99.       border: none;
  100.       outline: none;
  101.       right: 0;
  102.       transform: translate(-5px, 5px);
  103.     }
  104.  
  105.     #icon_mute,
  106.     #icon_unmute {
  107.       width: 35px;
  108.     }
  109.  
  110.     #h1 {
  111.       display: block;
  112.       text-align: center;
  113.       font-size: 30px;
  114.       font-weight: 900;
  115.       margin: 50px 0;
  116.       font-family: courier new;
  117.     }
  118.  
  119.     #range_container {
  120.       width: 680px;
  121.       border: 5px solid;
  122.       position: absolute;
  123.       left: 220px;
  124.       top: 20px;
  125.       background-color: black;
  126.       cursor: pointer;
  127.     }
  128.  
  129.     #range {
  130.       width: 20px;
  131.       height: 20px;
  132.       background-color: red;
  133.       transform: translate(-10px, -10px);
  134.       display: inline-block;
  135.       border-radius: 50%;
  136.       cursor: pointer;
  137.       position: absolute;
  138.     }
  139.   </style>
  140. </head>
  141.  
  142. <body>
  143.  
  144.   <div id='player_container'>
  145.     <audio id='audio'>
  146.       <source src='https://1xpto.netlify.app/Audio/audio1.mp3' type='audio/mp3'>
  147.     </audio>
  148.  
  149.     <div id='time_control'>
  150.       <button id='bt1'>0</button>
  151.       <button id='bt2'>0</button>
  152.     </div>
  153.  
  154.     <button onclick='Pause()' id='pause'><img id='icon_pause' src='https://1xpto.netlify.app/Icons/stop.png'></button>
  155.     <button onclick='Play()' id='play'><img id='icon_play' src='https://1xpto.netlify.app/Icons/unstop.png'></button>
  156.  
  157.     <button onclick='Mute()' id='mute'><img id='icon_mute' src='https://1xpto.netlify.app/Icons/mute.png'></button>
  158.     <button onclick='playSound()' id='unmute'><img id='icon_unmute' src='https://1xpto.netlify.app/Icons/unmute.png'></button>
  159.  
  160.     <div id='range_container'>
  161.       <span id='range'></span>
  162.     </div>
  163.  
  164.   </div>
  165.  
  166.   <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>
  167.  
  168.   <script>
  169.     audio.onloadedmetadata = function() {
  170.       foo = getComputedStyle(range_container).getPropertyValue("width");
  171.       var x = parseInt(foo);
  172.       length = x / audio.duration;
  173.     };
  174.     audio.ontimeupdate = function() {
  175.       range.style.left = audio.currentTime * length + "px";
  176.       bt1.innerHTML = audio.currentTime.toFixed(0);
  177.       timer = bt2.innerHTML = audio.duration.toFixed(0);
  178.       bam = timer - audio.currentTime + 1;
  179.       bam--;
  180.       bt2.innerHTML = bam.toFixed(0);
  181.     };
  182.     range_container.onmousedown = (event) => {
  183.       x = event.clientX - 425;
  184.       audio.currentTime = x / length;
  185.       range.style.left = audio.currentTime * length + "px";
  186.     };
  187.     onmousedown = (event) => {
  188.       range_container.onmousemove = function(event) {
  189.         x = event.clientX - 425;
  190.         audio.currentTime = x / length;
  191.         range.style.left = audio.currentTime * length + "px";
  192.       };
  193.     };
  194.     onmouseup = function() {
  195.       range_container.onmousemove = function() {
  196.         range.style.left = x;
  197.       };
  198.     };
  199.     play.onclick = () => {
  200.       if (audio.paused) {
  201.         audio.play();
  202.         play.style.display = "none";
  203.       }
  204.     };
  205.     pause.onclick = () => {
  206.       audio.pause();
  207.       play.style.display = "block";
  208.     };
  209.     unmute.onclick = () => {
  210.       audio.muted = true;
  211.       unmute.style.display = "none";
  212.     };
  213.     mute.onclick = () => {
  214.       audio.muted = false;
  215.       unmute.style.display = "block";
  216.     };
  217.     audio.onended = () => {
  218.       audio.play();
  219.     };
  220.   </script>
  221. </body>
  222.  
  223. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement