问题
I am trying to autoplay a background music to my website and start it playing at 10% volume. This is my code:
<audio id="bgAudio" autoplay src="music.mp3" volume="0.1"></audio>
This doesn't work for me. I've searched other ways on the internet but none of them worked. Can anyone help me?
回答1:
You have to use Javascript to modify the volume after defining it because volume isn't a valid property for the audio tag. It SHOULD be IMO but it isn't. Try this:
<audio autoplay id="bgAudio">
<source src="music.mp3" type="audio/mpeg">
</audio>
<script>
var audio = document.getElementById("bgAudio");
audio.volume = 0.1;
</script>
回答2:
Open the Developer Tools (Ctrl+Shift+I in Chrome) and check the Console tab, there should be some error messages. It seems that the problem is in wrong path to the .mp3 file.
来源:https://stackoverflow.com/questions/44141681/html-audio-volume-adjustment