Friday, January 10, 2025
Google search engine
HomeLanguagesJavascriptp5.MediaElement volume() Method

p5.MediaElement volume() Method

The volume() method of p5.MediaElement in p5.js is used to set or return the current volume of the media element. The volume can be specified with a value between 0.0 and 1.0, where 0.0 means no volume from the media, and the value of 1.0 means the maximum volume. Any intermediate value can be used for specifying the needed volume.

The current volume of the media can be returned by not passing any parameter to the function.

Syntax:

volume( val )

Parameters: This method accepts a single parameter as mentioned above and described below:

  • val: It is a number that specifies the volume to be set. It can be a volume between 0.0 and 1.0. It is an optional parameter.

Return Value: This method returns a Number that denotes the current volume of the media element.

The examples below illustrate the volume() method in p5.js:

Example 1: 

Javascript




let currVolume = 0.5;
  
function setup() {
    createCanvas(500, 400);
    textSize(18);
  
    text("Click on the buttons to increase " +
         "or decrease the volume", 20, 20);
  
    example_media =
      createVideo("sample-video.mp4");
    example_media.size(500, 300);
    example_media.position(20, 100);
    example_media.showControls();
  
    example_media.volume(currVolume);
  
    incBtn = createButton("Increase Volume");
    incBtn.position(30, 40);
    incBtn.mousePressed(incVol);
  
    decBtn = createButton("Decrease Volume");
    decBtn.position(160, 40);
    decBtn.mousePressed(decVol);
  
    text("The video is playing at " +
         "half volume", 20, 80);
}
  
function incVol() {
  clear();
  
  if (currVolume < 0.9)
    currVolume += 0.1;
  
  // Set the new higher volume
  example_media.volume(currVolume);
  
  text("The volume has been increased",
       20, 80);
  
  text("Click on the buttons to increase "
       "or decrease the volume", 20, 20);
}
  
function decVol() {
  clear();
  
  if (currVolume > 0.1)
    currVolume -= 0.1;
  
  // Set the new lower volume
  example_media.volume(currVolume);
  
  text("The volume has been decreased",
       20, 80);
  
  text("Click on the buttons to increase " +
       "or decrease the volume", 20, 20);
}


Output:

Example 2:

Javascript




function setup() {
  createCanvas(500, 400);
  textSize(20);
  
  text("Move the slider to increase or " +
       "decrease the volume", 20, 20);
  
  example_media =
    createVideo("sample-video.mp4");
  example_media.size(500, 300);
  example_media.position(20, 80);
  
  example_media.showControls();
  
  volSlider =
    createSlider(0.0, 1.0, 0.5, 0.1);
  volSlider.position(20, 40);
  volSlider.input(changeVol);
}
  
function changeVol() {
  clear();
  
  let volumeVal = volSlider.value();
  
  // Set the media volume to the slider value
  example_media.volume(volumeVal);
  
  // Get the current volume
  let currVolume = example_media.volume();
  
  text("Move the slider to increase or " +
       "decrease the volume", 20, 20);
  text("Video is playing at volume: " +
       currVolume, 20, 80);
}


Output:

Online editor: https://editor.p5js.org/
Environment Setup: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/
Reference: https://p5js.org/reference/#/p5.MediaElement/volume

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

Recent Comments