如何让游戏音量增加

问题描述 投票:0回答:1

声音功能 -

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的音量上升?

javascript html audio
1个回答
-1
投票

你只需要与volume property元素的audio进行交互。

这是我使用的文件中的一个示例函数,它为用户提供了一个向上和向下按钮,可以通过.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;
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.