Html audio volume adjustment

大兔子大兔子 提交于 2020-01-17 06:39:06

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!