【问题标题】:Randomised Audio List is not playing when called调用时不播放随机音频列表
【发布时间】:2016-03-05 09:48:19
【问题描述】:

功能:

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

做了什么:

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

问题:

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

Index.html:1221 Uncaught TypeError: PlaySound.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"];


 $('#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>

【问题讨论】:

    标签: javascript jquery audio random jplayer


    【解决方案1】:

    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>

    【讨论】:

    • 谢谢!但是只播放了一个随机音频,它没有播放整个列表,应该将get(0)编辑为.get(audioList)吗??
    • 您预计何时播放另一个音频? OnComplete 早期的音频?如果是,请相应编码..
    • 下一个音频将在之前播放的每个音频之后播放
    • 好的,但这会导致类型错误,因为 .src 将是未定义的
    • 我在发布之前测试了这段代码。你遇到了什么错误?
    猜你喜欢
    • 2014-02-25
    • 1970-01-01
    • 2013-07-17
    • 1970-01-01
    • 2023-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-24
    相关资源
    最近更新 更多