Static p5_sys::global::loadSound[][src]

pub static loadSound: LoadSoundInternalType
Expand description

loadSound() returns a new p5.SoundFile from a specified path. If called during preload(), the p5.SoundFile will be ready to play in time for setup() and draw(). If called outside of preload, the p5.SoundFile will not be ready immediately, so loadSound accepts a callback as the second parameter. Using a local server is recommended when loading external files.

Examples

let mySound;
function preload() {
  soundFormats('mp3', 'ogg');
  mySound = loadSound('assets/doorbell');
}

function setup() {
  let cnv = createCanvas(100, 100);
  cnv.mousePressed(canvasPressed);
  background(220);
  text('tap here to play', 10, 20);
}

function canvasPressed() {
  // playing a sound file on a user gesture
  // is equivalent to `userStartAudio()`
  mySound.play();
}

Parameters

path Path to the sound file, or an array with paths to soundfiles in multiple formats i.e. [‘sound.ogg’, ‘sound.mp3’]. Alternately, accepts an object: either from the HTML5 File API, or a p5.File.

successCallback? Name of a function to call once file loads

errorCallback? Name of a function to call if there is an error loading the file.

whileLoading? Name of a function to call while file is loading. This function will receive the percentage loaded so far, from 0.0 to 1.0.