Open In App

p5.js | loadSound() Function

Last Updated : 17 Jan, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

The p5.loadSound is an inbuilt function of p5.sound library that contains the path to the object that will playable as web audio. You can easily attach any file that supports the client’s browsers. So be careful to choose, better is to make an array of the file format that will be imported as the client’s browsers supported the format. Like mp3, ogg and wav, etc.

Syntax:

p5.loadSound(path, [successCallback], [errorCallback], [whileLoadingCallback])

Parameters: This function accepts four parameters as mentioned above and described below:

  • path: This parameter holds the path of the file as a string, you can add multiple files.
  • successCallback: This parameter holds the function when the file loads it is an optional parameter.
  • errorCallback: This parameter holds the function of error if the function you want to call by the previous parameter was a failure. If the performable function can’t be called this parameter will handle this matter and inform what was wrong. It is also an optional parameter.
  • whileLoadingCallback: This parameter holds the function that you want to call while the page is loading, and it’s optional also.

Below example illustrate the p5.loadSound() function in JavaScript:

Example:




var sound;
   
function preload() {
  
    // Initialize sound
    sound = loadSound("pfivesound.mp3");
}
   
function setup() {
  
    // Playing the preloaded sound
    sound.play();


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.loadSound function are listed below:

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Safari
  • Opera

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads