功能性:
随机音频在游戏页面中播放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>
&#13;
答案 0 :(得分:0)
audioList
不是音频元素,而是array
,它没有方法play()
将src
的{{1}}属性设置为随机audion文件,并将AudioElement
方法设置为id为play的元素。
试试这个:
play()
&#13;
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();
}
});
&#13;