p5.js | playMode() Function
Last Updated :
13 Feb, 2022
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.
Syntax:
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 play() 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:
Example:
html
var sound;
function preload() {
// Initialize sound
sound = loadSound("pfivesound.mp3");
}
function mouseClicked() {
// Playing the preloaded sound
sound.playMode('restart');
sound.play();
}
function setup() {
// Playing the preloaded sound
sound.playMode('sustain');
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.js playMode() function are listed below:
- Google Chrome
- Internet Explorer
- Firefox
- Safari
- Opera
Share your thoughts in the comments
Please Login to comment...