我目前正在使用JavaScript制作游戏,但是在尝试拍摄时,拍摄声音不会重新启动。我的html看起来像这样:
<embed id="playerShot" src="resources/sounds/ump45shot.mp3" hidden="true" autostart="false"loop="false" volume="20">
我的JavaScript功能是
function shoot() {
document.getElementById("playerShot").Play();
}
有什么方法可以重新播放声音?
如果您在JavaScript中制作游戏,我建议您选择JavaScript到HTML以加载和操纵您的资源,例如声音和图形。您可以阅读本文以开始使用Web Audio API:http://www.html5rocks.com/en/tutorials/webaudio/intro/。
我建议您使用<audio>
元素,我认为它比嵌入式标签更适合您的需求。https://developer.mozilla.org/en-us/docs/html/element/audio
倒带<audio>
标签:document.getElementById('iamanaudioelement').currentTime = 0
(旁注:并非所有浏览器都支持mp3 <audio>
元素。如果您打算让Firefox用户玩游戏,则应具有检测浏览器支持是否具有mp3支持,例如使用Modernizr,并创建OGG或MP3音频标签适当。)
这是您想要的吗?
<button onclick="playSfx('resources/sounds/ump45shot.mp3');">PLAY</button>
<!--Script-->
<script>
function playSfx(filename){
var snd = new Audio(filename);
snd.play();
}
</script>