javascript - 尝试在单击按钮时播放随机图像和随机声音

标签 javascript html button audio dom-events

图像在单击按钮时成功随机化,但是声音不会播放。我不知道音频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>

最佳答案

您必须创建一个类似的声音数组:

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>

关于javascript - 尝试在单击按钮时播放随机图像和随机声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61492777/

相关文章:

javascript - Bootstrap 下拉按钮高度不同

c# - 是否可以保持统一激活脚本?

javascript - 当按钮处于事件状态时从按钮中移除阴影

javascript - 禁用链接单击,直到页面加载

javascript - JSP JQuery 注销后显示 div

html - 在不使用绝对定位的情况下,上面有可变高度内容的页脚?

html - 更改 Bootstrap 模式的默认宽度

javascript - 如何像战舰游戏一样读取HTML表格?

javascript - 如果收到空数组,如何显示 "no result"

ios - 点击按钮执行某些操作,然后转到另一个 View Controller