Advertisement
1xptolevitico69

Javascript Audio localStorage

Jan 3rd, 2022
1,319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 4.26 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>Javascript Audio localStorage</title>
  9.   <style>
  10.  
  11.  
  12.     .clone {
  13.       position: absolute;
  14.       top: 10px;
  15.       right: 20px;
  16.       text-decoration: none;
  17.       background-color: red;
  18.       width: 150px;
  19.       text-align: center;
  20.       color: snow;
  21.       font-family: arial black;
  22.       padding: 5px 20px;
  23.     }
  24.  
  25.     .sub_clone {
  26.       position: absolute;
  27.       top: 50px;
  28.       right: 20px;
  29.       text-decoration: none;
  30.       background-color: red;
  31.       width: 150px;
  32.       text-align: center;
  33.       color: snow;
  34.       font-family: arial black;
  35.       padding: 5px 20px;
  36.     }
  37.  
  38.     body {
  39.       margin: 0;
  40.       background-color: ivory;
  41.       overflow-x: hidden;
  42.     }
  43.  
  44.     #box div {
  45.       cursor: context-menu;
  46.       background-color: moccasin;
  47.       padding: 30px 10px;
  48.       margin-top: 5px;
  49.       color: maroon;
  50.       font-size: 5vw;
  51.       transform: scaleX(3);
  52.       transform-origin: left;
  53.       font-family: Arial black;
  54.     }
  55.  
  56.     #box {
  57.       height: 80vh;
  58.       overflow-y: scroll;
  59.       overflow-x: hidden;
  60.     }
  61.  
  62.     audio {
  63.       border-radius: 5px;
  64.       position: fixed;
  65.       bottom: 30px;
  66.       width: 80%;
  67.       left: 10%;
  68.       height: 40px;
  69.       border: 1px solid;
  70.       background-color: rgba(128, 128, 128, 0.1);
  71.       box-shadow: 0 0 0 5px snow,
  72.         0 0 0 10px green;
  73.     }
  74.  
  75.     @media all and (max-width:425px) {
  76.       audio {
  77.         width: 100%;
  78.         left: 0;
  79.         box-shadow: none;
  80.         border: none;
  81.         border-radius: 0;
  82.       }
  83.     }
  84.   </style>
  85. </head>
  86.  
  87. <body>
  88.  
  89.   <div id='box'>
  90.     <div>Audio 1</div>
  91.     <div>Audio 2</div>
  92.     <div>Audio 3</div>
  93.     <div>Audio 4</div>
  94.     <div>Audio 5</div>
  95.     <div>Audio 6</div>
  96.     <div>Audio 7</div>
  97.   </div>
  98.  
  99.   <audio controls id='audio'></audio>
  100.  
  101.   <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>
  102.  
  103.   <a class='sub_clone' href='https://1xpto.netlify.app/video/video sample01.mp4'>VIDEO SAMPLE</a>
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.   <script>
  111.     a = 0;
  112.     x = [
  113.       'https://1xpto.netlify.app/audio/audio1.mp3',
  114.       'https://1xpto.netlify.app/audio/audio2.mp3',
  115.       'https://1xpto.netlify.app/audio/audio3.mp3',
  116.       'https://1xpto.netlify.app/audio/audio4.mp3',
  117.       'https://1xpto.netlify.app/audio/audio5.mp3',
  118.       'https://1xpto.netlify.app/audio/audio6.mp3',
  119.       'https://1xpto.netlify.app/audio/bass.mp3'
  120.     ];
  121.     audio.src = x[a];
  122.     box = document.getElementById('box').children;
  123.     document.onclick = (e) => {
  124.       for (i = 0; i < box.length; i++) {
  125.        if (box[i] == e.target) {
  126.          foo = i;
  127.          audio.src = x[foo];
  128.          audio.play();
  129.        }
  130.      }
  131.    }
  132.    audio.onended = () => {
  133.       box[foo].style.backgroundColor = 'moccasin';
  134.       if (foo == x.length - 1) {
  135.         audio.pause();
  136.         foo = 0;
  137.         audio.src = x[foo];
  138.         audio.play();
  139.       } else {
  140.         foo++;
  141.         audio.src = x[foo];
  142.         audio.play();
  143.       }
  144.     }
  145.     document.addEventListener('play', (e) => {
  146.       for (i = 0; i < box.length; i++) {
  147.        if (box[i] == e.target) {
  148.          box[i].style.backgroundColor = 'moccasin';
  149.        } else {
  150.          box[foo].style.backgroundColor = 'olive';
  151.        }
  152.      }
  153.    }, true);
  154.    document.addEventListener('click', (e) => {
  155.       for (i = 0; i < box.length; i++) {
  156.        if (box[i] == e.target) {
  157.          box[i].style.backgroundColor = 'olive';
  158.        } else {
  159.          box[foo].style.backgroundColor = 'moccasin';
  160.        }
  161.      }
  162.    }, true);
  163.    window.onbeforeunload = () => {
  164.       localStorage.setItem('key', audio.src);
  165.       localStorage.setItem('keyy', audio.currentTime);
  166.       localStorage.setItem('keyfoo', foo);
  167.     }
  168.     onload = () => {
  169.       audio.src = localStorage.getItem('key');
  170.       audio.currentTime = localStorage.getItem('keyy');
  171.       foo = localStorage.getItem('keyfoo');
  172.       box[foo].style.backgroundColor = 'olive';
  173.     }
  174.   </script>
  175. </body>
  176.  
  177. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement