p5.js | loadSound() Function

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.


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:






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


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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : skyridetim

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.