简体   繁体   English

调整音量 <audio> jQuery的元素

[英]Adjusting volume of an <audio> element with jQuery

Here's my HTML: 这是我的HTML:

<audio id="audio_core" autoplay="autoplay">
   <source src="audio/bgm.mp3" type="audio/mpeg">
   Your browser does not support the audio element.
</audio>

and my JavaScript: 和我的JavaScript:

$(document).ready(function() {
    $('#audio_core').prop('volume', 0.15);
}

I want to set the volume lower. 我想调低音量。

I guess you just need to properly close $(document).ready(function() with }) , the rest of the code works fine. 我猜您只需要适当地关闭$(document).ready(function()}) ,其余代码即可正常工作。

 $(document).ready(function() { $('#audio_core').prop('volume', 0.15); }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <audio id="audio_core" autoplay="autoplay" controls="controls"> <source src="http://www.maninblack.org/demos/PoliceLineDontCross.mp3" type="audio/mpeg"> Your browser does not support the audio element. </audio> 


PS: remove controls="controls" . PS:删除controls="controls" Added only to avoid refreshing the page to stop the music. 仅添加是为了避免刷新页面以停止音乐。

Found it. 找到了。 I had to do this: 我必须这样做:

<audio id="audio_core" autoplay="autoplay">
   <source src="audio/bgm.mp3" type="audio/mpeg">
       Your browser does not support the audio element.
</audio>
<script>
    $(document).ready(function() {
      $('#audio_core').prop('volume', 0.05);
    })
 </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM