调用时不播放随机音频列表

Randomised Audio List is not playing when called

功能:

随机音频在游戏页面播放 25 秒,然后在第 26 秒停止。

已完成:

我已经为音频文件创建了一个数组列表,一种随机化 audioList 的随机方法,最后播放随机音频。

问题:

音频未播放,显示以下错误消息;

Index.html:1221 Uncaught TypeError: PlaySound.play is not a function

 var audioList = ["lib/audio/Awesome.mp3", "lib/audio/Excellent.mp3", "lib/audio/Fantastic.mp3", "lib/audio/Great.mp3", "lib/audio/KeepitUp.mp3", "lib/audio/MatchGame.mp3", "lib/audio/MatchSpot1.mp3", "lib/audio/MatchSpot3.mp3"];


 $('#DefaultGamePage').fadeIn({
       duration: slideDuration,
       queue: false,
       complete: function() {

         $('#DefaultGameTimer').show();

         //Play Randomised Audio
         var random_Play Audio = Math.floor(Math.random() * (audioList.length));
         var PlaySound = audioList[random_PlayAudio];
         PlaySound.play();
         ...(other game method)..
       },
       1000)
<div id="GamePage" style="display:none; position:absolute; z-index:20; top:0px; left:0px; width: 1080px; height: 1920px; margin:auto;">

  <audio id="Play"></audio>
</div>

audioList 不是音频元素而是 array 并且它没有方法 play()

AudioElementsrc 属性 设置为随机音频文件,并对具有 id 的元素使用 play() 方法作为播放。

试试这个:

var audioList = ["lib/audio/Awesome.mp3", "lib/audio/Excellent.mp3", "lib/audio/Fantastic.mp3", "lib/audio/Great.mp3", "lib/audio/KeepitUp.mp3", "lib/audio/MatchGame.mp3", "lib/audio/MatchSpot1.mp3", "lib/audio/MatchSpot3.mp3"];

var playElem = $('#Play').get(0); //select audio element

var handler = function() {
  var random_PlayAudio = Math.floor(Math.random() * (audioList.length));
  playElem.src = audioList[random_PlayAudio];
  playElem.addEventListener("ended", handler); //on end of the audio, execute `handler` again
  playElem.play();
};


$('#DefaultGamePage').fadeIn({
  duration: slideDuration,
  queue: false,
  complete: function() {
    $('#DefaultGameTimer').show();
    handler();
  }
});
<div id="GamePage" style="display:none; position:absolute; z-index:20; top:0px; left:0px; width: 1080px; height: 1920px; margin:auto;">
  <audio id="Play"></audio>
</div>