admin管理员组

文章数量:1279015

I am working with

howler.js 2.0

but cant seem to get a simple example of updating the volume working. Here some a sample code:

window.sound = new Howl({
 src:'.mp3',
  loop: true,
  volume: 0.15
});

window.updateVolume = function(value) {
  alert('before update volume:', window.sound.volume());
  sound.volume = value;
  alert('after update volume:', window.sound.volume());
}

I have tried both using volume() function and just volume property. None seem to work.

JsFiddle: /

What am I missing? Also, I noticed that if you click play multiple times, multiple instances of the same sound start playing. I dont want that and hitting play on a particular howl instance should always work with that instance.

I am working with

howler.js 2.0

but cant seem to get a simple example of updating the volume working. Here some a sample code:

window.sound = new Howl({
 src:'http://example./assets/audio/background.mp3',
  loop: true,
  volume: 0.15
});

window.updateVolume = function(value) {
  alert('before update volume:', window.sound.volume());
  sound.volume = value;
  alert('after update volume:', window.sound.volume());
}

I have tried both using volume() function and just volume property. None seem to work.

JsFiddle: https://jsfiddle/umx2bdm8/

What am I missing? Also, I noticed that if you click play multiple times, multiple instances of the same sound start playing. I dont want that and hitting play on a particular howl instance should always work with that instance.

Share Improve this question edited May 29, 2016 at 0:53 Rajat asked May 29, 2016 at 0:45 RajatRajat 34.1k20 gold badges69 silver badges88 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

If you look at howler.js 2.0’s docs for volume, it shows that volume is a function, not an assignable property, and that you need to pass in the new volume as an argument to set the volume. So you need to do sound.volume(value) instead of sound.volume = value.

sound = new Howl({
  src: 'http://www.hochmuth./mp3/Tchaikovsky_Rococo_Var_orch.mp3',
  loop: true,
  volume: 0.5
});

updateVolume = function(value) {
  console.log('before update volume:', sound.volume());
  sound.volume(value);
  console.log('after update volume:', sound.volume());
}
<script src="https://cdnjs.cloudflare./ajax/libs/howler/2.0.0-beta13/howler.min.js"></script>


<button onclick="sound.play()">
  Play
</button>
<button onclick="updateVolume(0.15)">
  Change volume to 0.15
</button>

(I switched the volume values above for demo purposes, so listeners don’t have to worry whether the music will suddenly deafen them when they click the button.)

本文标签: javascripthowlerjs update volume of a soundStack Overflow