Javascript 如何提高游戏的音量
声音功能-Javascript 如何提高游戏的音量,javascript,html,audio,Javascript,Html,Audio,声音功能- function sound(src) { this.sound = document.createElement("audio"); this.sound.src = src; this.sound.setAttribute("preload", "auto"); this.sound.setAttribute("controls", "none"); this.
function sound(src) {
this.sound = document.createElement("audio");
this.sound.src = src;
this.sound.setAttribute("preload", "auto");
this.sound.setAttribute("controls", "none");
this.sound.style.display = "none";
document.body.appendChild(this.sound);
this.play = function(){
this.sound.play();
}
this.stop = function(){
this.sound.pause();
}
}
把声音放进去-
flying = new sound("flying.wav");
渲染声音-
flying.play()
我想知道的是,有没有办法使wav的音量增加?您只需与
音频元素的音量进行交互
下面是我使用的一个文件中的一个示例函数,该函数为用户提供了一个向上和向下按钮,以通过.1
在任意方向调整音量,并确保他们不会将音量设置为超出范围的值。这是不言自明的
function adjustVolume(increment) {
// Get the proposed new volume level and check that it
// is between 0 and 1 (inclusive) or you will throw an error.
var proposedV = audioElement.volume + increment;
if (proposedV >= 0 && proposedV <= 1) {
audioElement.volume = proposedV;
}
}
功能调整音量(增量){
//获取建议的新卷级别并检查
//介于0和1(包括)之间,否则将抛出错误。
建议的var V=音频元素。音量+增量;
如果(proposedV>=0&&proposedVaudioElement.volume=Math.min(Math.max(audioElement.volume+increment,0),1)
@PatrickRoberts当然可以,但优化代码是在使用代码之后才出现的。而且,我的方式让新手更容易阅读和理解。这不是优化,而是功能。如果卷===0.9
和增量===0.2
您不想取消更改,您想将其设置为1
什么audioElement是因为当我尝试它时,它说它没有定义,我会把它说成什么?@ScottMarcus实际上是你的解决方案,它的proposedV>=0&&proposedV