Advertisement
fahmihilmansyah

audi

Mar 1st, 2023
872
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.57 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <h2>My First JavaScript</h2>
  6.  
  7. <button type="button"
  8.        onclick="document.getElementById('demo').innerHTML = Date()">
  9.     Click me to display Date and Time.</button>
  10.  
  11. <p id="demo"></p>
  12.  
  13. <input type="text" id="txt">
  14. <button onclick="arraypush()">get</button>
  15. <p>Play: <i id="keberapa"></i></p>
  16. <script>
  17.  
  18.     let arrayaudio =[];
  19.     var isplay = false;
  20.     function play(audio, callback) {
  21.         audio.play();
  22.         if (callback) {
  23.             //When the audio object completes it's playback, call the callback
  24.             //provided
  25.             arrayaudio.shift();
  26.             audio.addEventListener('ended', callback);
  27.         }
  28.     }
  29.  
  30.     //Changed the name to better reflect the functionality
  31.     function play_sound_queue() {
  32.         var index = 0;
  33.         function recursive_play() {
  34.             //If the index is the last of the table, play the sound
  35.             //without running a callback after
  36.             // if (index + 1 === arrayaudio.length) {
  37.             //     play(arrayaudio[index], null);
  38.             // } else {
  39.                 //Else, play the sound, and when the playing is complete
  40.                 //increment index by one and play the sound in the
  41.                 //indexth position of the array
  42.                 play(arrayaudio[index], function() {
  43.                     index++;
  44.                     if (index + 1 === arrayaudio.length) {
  45.                         // play(arrayaudio[index], null);
  46.                         recursive_play();
  47.                     }
  48.                     // isplay = false;
  49.                 });
  50.             // }
  51.         }
  52.  
  53.         //Call the recursive_play for the first time
  54.         recursive_play();
  55.     }
  56.     /*
  57.     function recursive_play() {
  58.         var index = 0;
  59.         //Else, play the sound, and when the playing is complete
  60.         //increment index by one and play the sound in the
  61.         //indexth position of the array
  62.         play(arrayaudio[index], function() {
  63.             index++;
  64.             if (index + 1 === arrayaudio.length) {
  65.                 play(arrayaudio[index], null);
  66.                 isplay = false;
  67.             }else {
  68.                 recursive_play();
  69.             }
  70.         });
  71.     }*/
  72.     function arraypush(){
  73.         var txtisi = document.getElementById('txt').value;
  74.         arrayaudio.push(new Audio(txtisi));
  75.         console.log("1",txtisi);
  76.         if(!isplay){
  77.             isplay = true;
  78.             console.log("2",txtisi);
  79.             play_sound_queue();
  80.         }
  81.         alert(arrayaudio);
  82.     }
  83. </script>
  84. </body>
  85. </html>
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement