gpt4 book ai didi

javascript - 调用时未播放随机音频列表

转载 作者:行者123 更新时间:2023-12-03 01:55:49 25 4
gpt4 key购买 nike

功能:

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

已完成的操作:

我已经为音频文件创建了一个数组列表,这是一个随机化方法来对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()方法

srcAudioElement属性设置为随机audion文件,并对具有id作为play的元素使用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>

关于javascript - 调用时未播放随机音频列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35812585/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com