p5.js createAudio() Function Last Updated : 12 Jul, 2025 Comments Improve Suggest changes Like Article Like Report The createAudio() function is used to create an audio element in the DOM. The audio is created as a p5.MediaElement, which has methods for controlling the media and its playback. Syntax: createAudio(src, callback) Parameters: This function accept two parameters as mentioned above and described below: src: It is a string or an array of strings that specified path of the audio file. The array of strings can be used to specify multiple paths for the support of various browsers. callback: It is a callback function that would be fired when the 'canplaythrough' event fires. This event is fired when the audio has completed loading and does not require any additional buffering. It is an optional parameter. Return Value: It returns a pointer to the p5.MediaElement with the audio. Below examples illustrate the createAudio() function in p5.js: Example 1: javascript function setup() { createCanvas(300, 300); text("Click on the buttons below to"+ "play/pause the audio", 20, 20); audioElement = createAudio("sample_audio.wav"); audioElement.position(20, 50); audioElement.size(300); // Show the audio controls audioElement.showControls(); } Output: Example 2: javascript function setup() { createCanvas(300, 300); text("Loading the audio...", 20, 20); audioElement = createAudio("sample_audio.mp3", afterLoad); audioElement.position(20, 20); audioElement.size(300); playBtn = createButton("Play Audio"); playBtn.position(30, 80); playBtn.mouseClicked(playAudio); pauseBtn = createButton("Pause Audio"); pauseBtn.position(150, 80); pauseBtn.mouseClicked(pauseAudio); } function afterLoad() { text("The audio has finished loading and"+ " can now be played!", 20, 40); } function playAudio() { audioElement.play(); } function pauseAudio() { audioElement.pause(); } Output: Online editor: https://editor.p5js.org/ Environment Setup: https://www.geeksforgeeks.org/javascript/p5-js-soundfile-object-installation-and-methods/ Reference: https://p5js.org/reference/#/p5/createAudio Create Quiz Comment S sayantanm19 Follow 0 Improve S sayantanm19 Follow 0 Improve Article Tags : JavaScript Web Technologies JavaScript-p5.js Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings5 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in JavaScript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)8 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readScope of Variables in JavaScript3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like