2015-05-01 182 views
2

我想获得一些按钮来处理声音没有成功。对于我使用的页面我有它设置为这样:有音频播放点击

// javascript file for gun tutorial// 
 
window.onload=function() 
 
{ 
 
\t var extraAmmo = 210; 
 
\t var maxAmmo = 30; 
 
\t var currentAmmo = maxAmmo; 
 
\t 
 
\t var extraAmmoHud = document.getElementById("extra-ammo"); 
 
\t var currentAmmoHud = document.getElementById("current-ammo"); 
 
\t 
 
\t var shootButton = document.getElementById("shoot-button"); 
 
\t var unloadButton = document.getElementById("unload-button"); 
 
\t var reloadButton = document.getElementById("reload-button"); 
 
\t \t refreshScreen(); 
 
\t \t 
 
\t shootButton.onclick=function() 
 
\t { 
 
\t \t if(currentAmmo > 0) 
 
\t \t { 
 
\t \t \t currentAmmo--; 
 
\t \t \t refreshScreen(); 
 
\t \t } 
 
\t } 
 
\t unloadButton.onclick=function() 
 
\t { 
 
\t  if (currentAmmo > 0) 
 
\t \t { 
 
      unloadTimer = setTimeout(unloadButton.onclick, 150) 
 
\t   currentAmmo--; 
 
\t   refreshScreen(); 
 
\t  } 
 
\t \t else unloadTimer = null; 
 
\t } 
 
\t reloadButton.onclick=function() 
 
\t { 
 
\t \t var difference = getDifference(); 
 
\t \t if(extraAmmo >= difference) 
 
\t \t { 
 
\t \t \t currentAmmo += difference; 
 
\t \t \t extraAmmo -= difference; 
 
\t \t } 
 
\t \t else 
 
\t \t { 
 
\t \t \t currentAmmo += extraAmmo; 
 
\t \t \t extraAmmo -= extraAmmo; 
 
\t \t } 
 
\t \t refreshScreen(); 
 
\t \t 
 
\t \t function getDifference() 
 
\t \t { 
 
\t \t \t return maxAmmo -currentAmmo; 
 
\t \t } 
 
\t } 
 
\t function refreshScreen() 
 
\t { 
 
\t \t extraAmmoHud.innerHTML="Extra Ammo: " + extraAmmo; 
 
\t \t currentAmmoHud.innerHTML="Current Ammo: " + currentAmmo; 
 
\t } 
 
}
<!DOCTYPE HTML> 
 

 
<html> 
 
<head> 
 
<meta charset="UTF-8"> 
 
<title>Gun Tutorial</title> 
 
<link rel="stylesheet" type="text/css" href="css/style.css"/> 
 
<script src="js/gun.js"></script> 
 
<audio src="sounds/GunShot.mp3"></audio> 
 
<audio src="sounds/GunShotFullAuto.mp3"></audio> 
 
<audio src="sounds/GunCockingFast.wav"></audio> 
 
</head> 
 

 
<body> 
 
\t <div id="wrapper"> 
 
\t <header id="header"> 
 
\t \t <h1>Welcome to the Gun Show</h1> 
 
\t </header> 
 
\t <div id="content"></div> 
 
\t <form> 
 
\t <div id="boxobuttons"> 
 
\t \t <input type="button" value="Shoot" id="shoot-button" /> 
 
\t \t <input type="button" value="Unload" id="unload-button" /> 
 
\t \t <input type="button" value="Reload" id="reload-button" /> 
 
\t </div> 
 
\t 
 
\t \t <div id="ammo-count"> 
 
\t \t \t <p id="current-ammo"></p> 
 
\t \t \t <p id="extra-ammo"></p> 
 
\t \t </div> 
 
\t </form> 
 
\t <footer> 
 
\t </footer> 
 
\t </div> 
 
</body> 
 
</html>

我想看看我是否需要有声音充当一个变量,每个变量可再插入到不同的变量(shootButton,unloadButton和reloadButton)中,还是我必须完全分开做一些事情?

回答

1

为什么不加载你的声音只是在JavaScript?

例如:var gunshot = new Audio('GunShot.mp3');

如果你然后让你的声音,你可以将它们放在你的代码之间是这样的:

shootButton.onclick=function() 
    { 
     if(currentAmmo > 0) 
     { 
      currentAmmo--; 
      gunshot.play(); 
      refreshScreen(); 
     } 
    } 

我希望我回答你的问题与此有关。

+1

感谢输入托马斯,给我一个时刻,我会尽快与你,告诉你,如果它工作。 –

+1

它看起来像一个不,暂时,我想说我发现了问题,因为它可能不喜欢音频文件在它自己的文件夹(声音),所以我要将它们移动到打开index.html查看是否有效。 –

+1

将它移动到open.html与index.html工作,现在做同样的卸载和重新加载 –

0

从这段代码我不清楚你如何试图播放你的音频文件。音频标签主要用于嵌入用户控制的媒体。

如果您正在使用短音频文件进行幕后操作,我建议您直接使用Web Audio API,理想情况下使用bufferLoader对象为您的音频提供服务。这篇文章的前半部分介绍你需要知道的一切:

Getting Started with Web Audio API

+0

呃......当然,如果你不关心W3C标准或支持所有浏览器,并且你喜欢复杂性。 '