Thursday, September 4, 2025
HomeLanguagesJavascriptp5.js | setVolume() Function

p5.js | setVolume() Function

The setVolume() function is an inbuilt function in p5.js library. This function is used to control the volume of the played audio on the web. This function has a range of between (0.0) which means total silence to (1.0) which means full volume. This volume also can be controllable by a slider var by dividing that in different ranges.
 

Syntax:  

setVolume( volume, rampTime, timeFromNow )

Note: All the sound-related functions only work when the sound library is included in the head section of the index.html file.
Parameter: This function accept three parameters as mentioned above and described below.  

  • volume: This parameter holds a float number that defines the volume of the playback.
  • rampTime: This parameter holds an integer value of time in the second format after that the sound will be fade. It is optional.
  • timeFromNow: This parameter holds an integer value of time in the second format after that define event will happen.

Below examples illustrate the p5.setVolume() function in JavaScript: 
Example 1: In this example, we set the fixed volume in the code which is 0.5.  

javascript




var sound;
 
function preload() {
 
    // Initialize sound
    sound = loadSound("pfivesound.mp3");
}
 
function setup() {
 
    // Playing the preloaded sound
    sound.play();
    //stopping the played sound after 5 seconds
    sound.setVolume(0.5);
}


Example 2: In this example, we will create a slide that will help the user to increase the volume by 0.2, and the starting volume is set to 0.2. 

javascript




var sound;
var slider;
 
function preload() {
  
    // Initialize sound
    sound = loadSound("pfivesound.mp3");
}
  
function setup() {
  
    // Playing the preloaded sound
    sound.play();
    //creating sound rocker
    slider = createSlider(0, 1, 0.2, 0.2);
  
}
  
function draw() {
    sound.setVolume(slider.value());
}


Online editor: https://editor.p5js.org/ 
Environment Setup: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/
Supported Browsers: The browsers are supported by p5.js setVolume() function are listed below:  

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Safari
  • Opera 
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32264 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6629 POSTS0 COMMENTS
Nicole Veronica
11799 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11858 POSTS0 COMMENTS
Shaida Kate Naidoo
6749 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6698 POSTS0 COMMENTS
Umr Jansen
6716 POSTS0 COMMENTS