亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

嘗試在按鈕單擊時播放隨機圖像和隨機聲音

嘗試在按鈕單擊時播放隨機圖像和隨機聲音

LEATH 2022-07-21 09:56:54
圖像在按鈕單擊時成功隨機化,但不會播放聲音。我不知道音頻 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>
查看完整描述

1 回答

?
墨色風雨

TA貢獻1853條經驗 獲得超6個贊

您必須像這樣創建一組聲音:


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>


查看完整回答
反對 回復 2022-07-21
  • 1 回答
  • 0 關注
  • 97 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號