求JS控制bgsound的方法

JavaScript014

求JS控制bgsound的方法,第1张

地址可以通过修改它的src属性来改变,音量可以使用它的volume 来改变,它的范围为-10000到0。

举一下改变声音的例子吧。

<SCRIPT Language="VBScript">

Function btnUp_OnClick()

Dim iVolume

iVolume = document.all.oSound.volume + 500

If iVolume <0 Then

document.all.oSound.volume = iVolume

End If

End Function

Function btnDown_OnClick()

Dim iVolume

iVolume = document.all.oSound.volume - 500

If iVolume >-10000 Then

document.all.oSound.volume = iVolume

End If

End Function

Function btnSilence_OnClick()

document.all.oSound.volume = -10000

End Function

</SCRIPT>

<bgSound src="音乐地址" id="oSound"/>

<button name="btnUp">加大</button>

<button name="btnDown">减小</button>

<button name="btnSilence">静音</button>

---------------------------------------------

你定义了一个变量,但没有赋值,所以不行,你需要对它进行赋值,如:

Dim iVolume

iVolume = document.all.oSound.volume

这样才能进行比较。

GainNode

<input type="range" min="0" max="100" id="volume" /><script type="text/javascript">

    var tank = new Audio("http://qianduannotes.duapp.com/file/tankWar.mp3")

    tank.loop = true    var mario = new Audio("http://qianduannotes.duapp.com/file/SuperMario.mp3")

    mario.loop = true    var AudioContext = AudioContext || webkitAudioContext    var context = new AudioContext()    var source1 = context.createMediaElementSource(tank)    var source2 = context.createMediaElementSource(mario)    var gain1 = context.createGain()    var gain2 = context.createGain()    //连接:source → gain → destination    source1.connect(gain1)

    source2.connect(gain2)

    gain1.connect(context.destination)

    gain2.connect(context.destination)    //音量控制

    var value

    onload = volume.onchange = function(){

      gain1.gain.value = volume.value / 100

      gain2.gain.value = 1 - volume.value / 100

    }

    tank.onload = mario.onlond = function(){

        console.log("var1, var2")

    }

    tank.play()

    mario.play()</script>