试图在点击按钮时播放随机图像和随机声音。

问题描述 投票:0回答:1

点击按钮时图片随机化成功,但声音无法播放。我不知道音频JS有什么问题,但它一直让我感到困惑。我想在点击同一个按钮的时候,让两者随机执行。我使用Chrome浏览器来测试我的工作。

<script type = "text/javascript">
        //Create random picture array
            
        function imgchange() {

            var myImages1 = new Array();
            myImages1[1] = "Matthew1.jpg";
            myImages1[2] = "Matthew2.jpg";
            myImages1[3] = "Matthew3.jpg";
            myImages1[4] = "Matthew4.jpg"; //Image Array
            myImages1[5] = "Matthew5.jpg";
            myImages1[6] = "Matthew6.jpg";
            myImages1[7] = "Matthew7.jpg";
            var rnd = Math.floor(Math.random() * myImages1.length);// Random Choice of Image
            if (rnd == 0) {
                rnd = 1;
            }

            document.getElementById("gen-img").src = myImages1[rnd];//Gets Image
       
        }      
        function playRandomSound() {

            //An array to house all of the URLs of your sounds
            var sounds = new Audio["sound1.mp3", "sound2.mp3", "sound3.mp3", "sound4.mp3"];

            //This line will select a random sound to play out of your provided URLS
            var soundFile = sounds[Math.floor(Math.random() * sounds.length)];

            //Find the player element that you created and generate an embed file to play the sound within it
            document.getElementById("Button").innerHTML = "<embed src=\"" + soundfile + "\" hidden=\"true\" autostart=\"true\" loop=\"false\" />";

    </script>
<body style="height: 663px">
    
    <div class="Title" id="title">Alright! Alright! Alright!</div>

    <p><img id="gen-img" src="img/who/1.jpg"></p>
   
 
    <p> <input id="Button" type="button" value="Random" onclick="imgchange(); playRandomSound();"/></p>

    </body>
javascript html button audio javascript-events
1个回答
0
投票

你要创建一个声音的数组,像这样。

var sounds = ["sound1.mp3", "sound2.mp3", "sound3.mp3", "sound4.mp3"].map(
  (sound) => new Audio(sound)
);

你还有一个错别字,在开头的那一行 document.getElementById("Button").innerHTML, soundFile 有资本 F. 虽然我觉得你不需要那条线,但你可以通过呼叫来播放声音 play() 上,像 soundFile.play()请检查下面的片段。

//Create random picture array

function imgchange() {
  var myImages1 = new Array();
  myImages1[1] = "Matthew1.jpg";
  myImages1[2] = "Matthew2.jpg";
  myImages1[3] = "Matthew3.jpg";
  myImages1[4] = "Matthew4.jpg"; //Image Array
  myImages1[5] = "Matthew5.jpg";
  myImages1[6] = "Matthew6.jpg";
  myImages1[7] = "Matthew7.jpg";
  var rnd = Math.floor(Math.random() * myImages1.length); // Random Choice of Image
  if (rnd == 0) {
    rnd = 1;
  }

  document.getElementById("gen-img").src = myImages1[rnd]; //Gets Image
}

function playRandomSound() {

  //An array to house all of the URLs of your sounds
  var sounds = [
    new Audio(
      "https://interactive-examples.mdn.mozilla.net/media/examples/t-rex-roar.mp3"
    ),
  ];

  // var sounds = new Audio([
  //   ("sound1.mp3", "sound2.mp3", "sound3.mp3", "sound4.mp3")
  // ]();

  //This line will select a random sound to play out of your provided URLS
  var soundFile = sounds[Math.floor(Math.random() * sounds.length)];
  soundFile.play()

  //Find the player element that you created and generate an embed file to play the sound within it
  document.getElementById("Button").innerHTML =
    '<embed src="' +
    soundFile +
    '" hidden="true" autostart="true" loop="false" />';
}
<div class="Title" id="title">Alright! Alright! Alright!</div>

<p><img id="gen-img" src="img/who/1.jpg" /></p>

<p>
  <input id="Button" type="button" value="Random" onclick="imgchange(); playRandomSound();" />
</p>
© www.soinside.com 2019 - 2024. All rights reserved.