admin管理员组文章数量:1291008
I have a page that has some animal images and when you click on them it plays the sound of the animal, but since its a kid's game I have a background music, but the sound is too loud. I want when the background music plays automatically, the volume changes to 0.5. How can I set a function that does that? I don't want a function based on click, I want it hidden and change the volume automatically.
The function (it's not working)
myAudio = document.getElementById("audio1");
function setHalfVolume() {
myAudio.volume = 0.4;
}
HTML
<audio id= "audio1" controls="controls" onload="setHalfVolume()">
<source src="Audio\Jaunty Gumption.mp3" type="audio/mp3">
</audio>
I have a page that has some animal images and when you click on them it plays the sound of the animal, but since its a kid's game I have a background music, but the sound is too loud. I want when the background music plays automatically, the volume changes to 0.5. How can I set a function that does that? I don't want a function based on click, I want it hidden and change the volume automatically.
The function (it's not working)
myAudio = document.getElementById("audio1");
function setHalfVolume() {
myAudio.volume = 0.4;
}
HTML
<audio id= "audio1" controls="controls" onload="setHalfVolume()">
<source src="Audio\Jaunty Gumption.mp3" type="audio/mp3">
</audio>
Share
Improve this question
edited Dec 5, 2013 at 2:53
PhearOfRayne
5,0503 gold badges33 silver badges44 bronze badges
asked Dec 5, 2013 at 2:45
Fran MartinezFran Martinez
6894 gold badges18 silver badges36 bronze badges
1
- Try initialize myAudio inside function function setHalfVolume() { myAudio = document.getElementById("audio1"); myAudio.volume = 0.4; } – Tamil Selvan C Commented Dec 5, 2013 at 2:56
2 Answers
Reset to default 5The event you need is called onloadeddata.
Also unless you need access to the myAudio variable from other functions or globally I would suggest moving it into the setHalfVolume
function. Give this a try.
Change your HTML to:
<audio id= "audio1" controls="controls" onloadeddata="setHalfVolume()">
<source src="Audio\Jaunty Gumption.mp3" type="audio/mp3">
</audio>
Change your JavaScript to:
function setHalfVolume() {
var myAudio = document.getElementById("audio1");
myAudio.volume = 0.5; //Changed this to 0.5 or 50% volume since the function is called Set Half Volume ;)
}
Try adding an event for play
on all of the audio elements of interest and calling setHalfVolume in there:
document.querySelector('#audio1').addEventListener('play', setHalfVolume);
You'll also probably want to reset the volume back to normal after
document.querySelector('#audio1').addEventListener('pause', resetVolume);
Where resetVolume
and setHalfVolume
are declared sort of like:
function setHalfVolume() {
document.getElementById("audio1").volume /= 2;
}
function resetVolume() {
document.getElementById("audio1").volume *= 2;
}
本文标签: javascriptLower background music volume when autoplay HTMLStack Overflow
版权声明:本文标题:javascript - Lower background music volume when autoplay HTML - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741509682a2382533.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论