Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何提高游戏的音量_Javascript_Html_Audio - Fatal编程技术网

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&&proposedV
audioElement.volume=Math.min(Math.max(audioElement.volume+increment,0),1)
@PatrickRoberts当然可以,但优化代码是在使用代码之后才出现的。而且,我的方式让新手更容易阅读和理解。这不是优化,而是功能。如果
卷===0.9
增量===0.2
您不想取消更改,您想将其设置为
1
什么audioElement是因为当我尝试它时,它说它没有定义,我会把它说成什么?@ScottMarcus实际上是你的解决方案,它的
proposedV>=0&&proposedV