简体   繁体   中英

Adjusting volume of an <audio> element with jQuery

Here's my 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:

$(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() { $('#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" . 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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