如何为此添加音量值?

时间:2016-07-17 09:52:49

标签: javascript audio

这就是我正在使用的:

var numberOfSongs = 5
var sound = new Array(numberOfSongs+1)
sound[0]= "https://domain.com/media/audio1.mp3"
sound[1]= "https://domain.com/media/audio2.mp3"
sound[2]= "https://domain.com/media/audio3.mp3"
sound[3]= "https://domain.com/media/audio4.mp3"
sound[4]= "https://domain.com/media/audio5.mp3"
function randomNumber(){
var randomLooper = -1
while (randomLooper < 0 || randomLooper > numberOfSongs ||  isNaN(randomLooper)){ randomLooper = parseInt(Math.random()*(numberOfSongs+1))
}
return randomLooper
}
var randomsub = randomNumber()
var soundFile = sound[randomsub]
document.write ('<EMBED src= "' + soundFile + '" hidden=true  autostart=true loop=true>')

一位朋友在我的网站上给了我这个。这用于从其中5个列表中随机选择的mp3文件。我正在尝试为此添加音量值(没有任何可见的显示为控件)但是要作为JS添加。 我希望能够上传音频,但不必在音频编辑器中编辑音频本身,为了方便起见,我宁愿在JS中这样做。为此添加音量值的好方法是什么?

例如:默认情况下的音量设置为25%,但除非您编辑此JS的源代码,否则无法更改。应该没有可见的控件。

我根本不擅长JS。我已经查找了如何构建这样的东西,但我不知道从哪里开始。

1 个答案:

答案 0 :(得分:1)

使用Audio

var audio = new Audio(soundFile);
audio.loop = true;
audio.autoplay = true;
// set volume here, between 0 and 1
audio.volume = .7;