p5.js | playMode() Function

The playMode() function is an inbuilt function in p5.js library. This function is used to define what is happening with the loaded audio if it is triggered while in the middle of playback or not. This function is useful when you want to restart the sound during the loading time of the website, without this function the sound can be played on mouseclick or keypressed action but that does not stop the previously played sound.


playMode( string )

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 a single parameter string that has two values mentioned above and described below, the default value is ‘sustain’:

  • string: This parameter has two values those are restart In this mode the paly() will stop playback and start over. The sustain In this mode playback will continue simultaneously to the new playback.

Below example illustrates the p5.js playMode() function in JavaScript:





var sound; 
function preload() { 
    // Initialize sound 
    sound = loadSound("pfivesound.mp3"); 
function mouseClicked() { 
    // Playing the preloaded sound 
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.js playMode() 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.

Article Tags :

Be the First to upvote.

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