The setVolume()
method is used to set the volume of an Audio
object in the Three.js library. The Audio
object represents an audio file that can be played in a Three.js scene.
audio.setVolume( volume )
audio
- The Audio
object whose volume is to be set.volume
- A number between 0 and 1 that represents the new volume for the Audio
object. A value of 0 will completely mute the audio, while a value of 1 will set the volume to maximum.var listener = new THREE.AudioListener();
var sound = new THREE.Audio( listener );
var audioLoader = new THREE.AudioLoader();
audioLoader.load( 'sounds/song.mp3', function( buffer ) {
sound.setBuffer( buffer );
sound.setLoop( true );
sound.setVolume( 0.5 );
sound.play();
});
In this example, an Audio
object is created and loaded with a song file using the AudioLoader
object. The setVolume()
method is then used to set the volume of the sound
object to 50% (0.5).
Audio
object can be changed at any time using the setVolume()
method.setVolume()
method does not affect the volume of the computer or device playing the audio. It only affects the volume of the Audio
object within the Three.js scene.setVolume()
method can be used to fade in or out an Audio
object by gradually increasing or decreasing the volume over time.