Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
103 views
in Technique[技术] by (71.8m points)

html - Javascript Audio Play() function not playing every sound

I want to play a series of sound (9-10 tracks) after I go to my website. However right now, the sounds are not played correctly. Sometimes, some of the tracks at the end are not played. Sometimes, all of the tracks are played.

Here is my code:

<script>   
          
function init() {
    var sample = document.getElementById("sample");
    sample.addEventListener("ended", function() {
        var ban1 = document.getElementById("ban1");
        ban1.play();
    });


    var ban1 = document.getElementById("ban1");
    ban1.addEventListener("ended", function() {
        var audio1 = document.getElementById("audio1");
        audio1.play();
    });
    var audio1 = document.getElementById("audio1");
    audio1.addEventListener("ended", function() {
        var silence1 = document.getElementById("silence1");
        silence1.play();
    });
    var silence1 = document.getElementById("silence1");


    silence1.addEventListener("ended", function() {
        var ban2 = document.getElementById("ban2");
        ban2.play();
    });
    var ban2 = document.getElementById("ban2");
    ban2.addEventListener("ended", function() {
        var audio2 = document.getElementById("audio2");
        audio2.play();
    });
    var audio2 = document.getElementById("audio2");
    audio2.addEventListener("ended", function() {
        var silence2 = document.getElementById("silence2");
        silence2.play();
    });
    var silence2 = document.getElementById("silence2");



    silence2.addEventListener("ended", function() {
        var ban3 = document.getElementById("ban3");
        ban3.play();
    });
    var ban3 = document.getElementById("ban3");
    ban3.addEventListener("ended", function() {
        var audio3 = document.getElementById("audio3");
        audio3.play();
    });
    var audio3 = document.getElementById("audio3");
    audio3.addEventListener("ended", function() {
        var silence3 = document.getElementById("silence3");
        silence3.play();
    });
    var silence3 = document.getElementById("silence3");


    silence3.addEventListener("ended", function() {
        var ban4 = document.getElementById("ban4");
        ban4.play();
    });
    var ban4 = document.getElementById("ban4");
    ban4.addEventListener("ended", function() {
        var audio4 = document.getElementById("audio4");
        audio4.play();
    });
    var audio4 = document.getElementById("audio4");
    audio4.addEventListener("ended", function() {
        var silence4 = document.getElementById("silence4");
        silence4.play();
    });
    var silence4 = document.getElementById("silence4");


    silence4.addEventListener("ended", function() {
        var ban5 = document.getElementById("ban5");
        ban5.play();
    });
    var ban5 = document.getElementById("ban5");
    ban5.addEventListener("ended", function() {
        var audio5 = document.getElementById("audio5");
        audio5.play();
    });
    var audio5 = document.getElementById("audio5");
    audio5.addEventListener("ended", function() {
        var silence5 = document.getElementById("silence5");
        silence5.play();
    });

</script> 

<body onload="init();">
<audio id="ban1">
          <source src="{{ url(/audio/banFile1.mp3 }}">
        </audio>
        <audio id="audio1">
          <source src="{{ url(/audio/file1.mp3) }}">
        </audio>
        <audio id="silence1">
          <source src="{{ url(/audio/silence/silence.mp3) }}">
        </audio>

        <audio id="ban2">
          <source src="{{ url(/audio/banFile2.mp3   }}">
        </audio>
        <audio id="audio2">
          <source src="{{ url(/audio/file2.mp3) }}">
        </audio>
        <audio id="silence2">
          <source src="{{ url('/audio/silence/'.$data[1]->getSilence()) }}">
        </audio>


        <audio id="ban3">
          <source src="{{ url(/audio/banFile3.mp3 ) }}">
        </audio>
        <audio id="audio3">
          <source src="{{ url(/audio/file3.mp3) }}">
        </audio>
        <audio id="silence3">
          <source src="{{ url('/audio/silence/'.$data[2]->getSilence()) }}">
        </audio>

        


        <audio id="ban4">
          <source src="{{ url(/audio/banFile4.mp3 ) }}">
        </audio>
        <audio id="audio4">
          <source src="{{ url(/audio/file4.mp3) }}">
        </audio>
        <audio id="silence4">
          <source src="{{ url('/audio/silence/'.$data[3]->getSilence()) }}">
        </audio>



        <audio id="ban5">
          <source src="{{ url(/audio/banFile5.mp3  ) }}">
        </audio>
        <audio id="audio5">
          <source src="{{ url(/audio/file5.mp3) }}">
        </audio>
        <audio id="silence5">
          <source src="{{ url('/audio/silence/'.$data[4]->getSilence()) }}">
        </audio>
 </body>  

I think maybe because the function started when all the sounds file are not loaded fully but I already used onload. Any help would be appreciated.

question from:https://stackoverflow.com/questions/65947407/javascript-audio-play-function-not-playing-every-sound

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Click the screen since autoplay requires that you mute your audio without user action. This example plays audio in the order your audioSrcs Array is in. Click the screen again and it advances automatically. It loops forever, through multiple audio files, but it's just an example. The important thing is that you assign your audio .srcs after your Events. Of course, you'll want to pay attention to the Promise structure as well.

//<![CDATA[
/* js/external.js */
let doc, htm, bod, nav, M, I, mobile, S, Q;
addEventListener('load', ()=>{
doc = document; htm = doc.documentElement; bod = doc.body; nav = navigator; M = tag=>doc.createElement(tag); I = id=>doc.getElementById(id);
mobile = nav.userAgent.match(/Mobi/i) ? true : false;
S = (selector, within)=>{
  let w = within || doc;
  return w.querySelector(selector);
}
Q = (selector, within)=>{
  let w = within || doc;
  return w.querySelectorAll(selector);
}
// tiny library above - magic below (can put on separate page using a load Event)
const audioSrcs = ['https://www.freesoundslibrary.com/wp-content/uploads/2017/11/duck-quack.mp3', 'https://www.freesoundslibrary.com/wp-content/uploads/2017/09/Cow-moo-sound.mp3', 'https://www.freesoundslibrary.com/wp-content/uploads/2017/11/cruise-ship-horn-sound.mp3', 'https://www.freesoundslibrary.com/wp-content/uploads/2018/03/tada-sound.mp3'];
const audioNodes = [], audioPromises = [];
let playing, l = audioSrcs.length, x = l-1;
for(let i=0,q=1,n,p; i<l; i++,q++){
  // Make audio Elements and push to audioNodes
  n = M('audio'); audioNodes.push(n);
  if(i !== x){
    n.onended = ()=>{
      audioNodes[q].play();
    }
  }
  else{
    n.onended = ()=>{
      audioNodes[0].play();
    }
  }
  n.onplay = ()=>{
    playing = i;
  }
  p = new Promise(resolve=>{ // wrap in Promise
    n.oncanplay = ()=>{
      resolve();
    }
  });
  // .src shoud be assigned after Events are assigned
  audioPromises.push(p); n.src = audioSrcs[i];
}
Promise.all(audioPromises).then(()=>{
  onclick = ()=>{
    if(playing === undefined){
      audioNodes[0].play();
    }
    else{
      audioNodes[playing].load();
      if(playing === x)playing = -1;
      audioNodes[playing+1].play();
    }
  }
});
}); // end load
//]]>
<!DOCTYPE html>
<html lang='en'>
  <head>
    <meta charset='UTF-8' /><meta name='viewport' content='width=device-width, height=device-height, initial-scale:1, user-scalable=no' />
    <title>Title Here</title>
    <link type='text/css' rel='stylesheet' href='css/external.css' />
    <script src='js/external.js'></script>
  </head>
<body>
  <div class='main'>
    Click the screen since autoplay requires that you mute your audio without user action. This example plays audio in the order your audioSrcs Array is in. Click the screen again and it advances automatically. It loops forever, through multiple audio files, but it's just an example. The important thing is that you assign your audio `.src`s after your Events. Of course, you'll want to pay attention to the `Promise` structure as well.
  </div>
</body>
</html>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...