Player
class Source ↳ extends Source<PlayerOptions>
Player is an audio file player with start, loop, and stop functions.
const player = new Tone.Player("https://tonejs.github.io/audio/berklee/gong_1.mp3").toDestination();
// play as soon as the buffer is loaded
player.autostart = true;Constructor
Section titled “Constructor”new Player(url?: string | ToneAudioBuffer | AudioBuffer, onload?: () => void)Parameters
-
url
string | ToneAudioBuffer | AudioBufferoptionalEither the AudioBuffer or the url from which to load the AudioBuffer
-
onload
() => voidoptionalThe function to invoke when the buffer is loaded.
new Player(options?: Partial<PlayerOptions>)Parameters
- options
Partial<PlayerOptions>optional
Properties
Section titled “Properties”autostart
Section titled “autostart”autostart: booleanIf the file should play as soon as the buffer is loaded.
onstop
Section titled “onstop”↳ inherited from Source
onstop: onStopCallbackThe callback to invoke when the source is stopped.
volume
Section titled “volume”↳ inherited from Source
volume: Param<"decibels">The volume of the output in decibels.
const source = new Tone.PWMOscillator().toDestination(); source.volume.value = -6;
loop: booleanIf the buffer should loop once its over.
const player = new Tone.Player("https://tonejs.github.io/audio/drum-samples/breakbeat.mp3").toDestination();
player.loop = true;
player.autostart = true;loopStart
Section titled “loopStart”loopStart: TimeIf loop is true, the loop will start at this position.
↳ inherited from Source
mute: booleanMute the output.
const osc = new Tone.Oscillator().toDestination().start(); // mute the output osc.mute = true;
playbackRate
Section titled “playbackRate”playbackRate: numberNormal speed is 1. The pitch will change with the playback rate.
const player = new Tone.Player("https://tonejs.github.io/audio/berklee/femalevoices_aa2_A5.mp3").toDestination();
// play at 1/4 speed
player.playbackRate = 0.25;
// play as soon as the buffer is loaded
player.autostart = true;progress
Section titled “progress”read-only
progress: numberDisplays the elapsed seconds since the player was started, taking into account playbackRate changes.
const player = new Tone.Player("https://tonejs.github.io/audio/berklee/gong_1.mp3", () => {
player.start();
setInterval(() => {
console.log(player.progress);
}, 100);
}).toDestination();reverse
Section titled “reverse”reverse: booleanIf the buffer should be reversed. Note that this sets the underlying ToneAudioBuffer.reverse, so if multiple players are pointing at the same ToneAudioBuffer, they will all be reversed.
const player = new Tone.Player("https://tonejs.github.io/audio/berklee/chime_1.mp3").toDestination();
player.autostart = true;
player.reverse = true;read-only ↳ inherited from Source
state: BasicPlaybackStateReturns the playback state of the source, either “started” or “stopped”.
const player = new Tone.Player("https://tonejs.github.io/audio/berklee/ahntone_c3.mp3", () => {
player.start();
console.log(player.state);
}).toDestination();Methods
Section titled “Methods”load()
Section titled “load()”load(url: string): Promise<Player>Load the audio file as an audio buffer. Decodes the audio asynchronously and invokes the callback once the audio buffer loads. Note: this does not need to be called if a url was passed in to the constructor. Only use this if you want to manually load a new url.
Parameters
-
url
stringThe url of the buffer to load. Filetype support depends on the browser.
Returns Promise<Player>
restart()
Section titled “restart()”Stop and then restart the player from the beginning (or offset)
Parameters
Returns this
seek()
Section titled “seek()”Seek to a specific time in the player’s buffer. If the source is no longer playing at that time, it will stop.
const player = new Tone.Player("https://tonejs.github.io/audio/berklee/gurgling_theremin_1.mp3", () => {
player.start();
// seek to the offset in 1 second from now
player.seek(0.4, "+1");
}).toDestination();Parameters
Returns this
setLoopPoints()
Section titled “setLoopPoints()”Set the loop start and end. Will only loop if loop is set to true.
const player = new Tone.Player("https://tonejs.github.io/audio/berklee/malevoices_aa2_F3.mp3").toDestination();
// loop between the given points
player.setLoopPoints(0.2, 0.3);
player.loop = true;
player.autostart = true;Returns this
start()
Section titled “start()”Play the buffer at the given startTime. Optionally add an offset and/or duration which will play the buffer from a position within the buffer for the given duration.
Parameters
Returns this
stop()
Section titled “stop()”↳ inherited from Source
stop(time?: Time): thisStop the source at the specified time. If no time is given, stop the source now.
const source = new Tone.Oscillator().toDestination();
source.start();
source.stop("+0.5"); // stops the source 0.5 seconds from nowParameters
-
time
TimeoptionalWhen the source should be stopped.
Returns this
sync()
Section titled “sync()”↳ inherited from Source
sync(): thisSync the source to the Transport so that all subsequent
calls to start and stop are synced to the TransportTime
instead of the AudioContext time.
const osc = new Tone.Oscillator().toDestination(); // sync the source so that it plays between 0 and 0.3 on the Transport's timeline osc.sync().start(0).stop(0.3); // start the transport. Tone.Transport.start(); // set it to loop once a second Tone.Transport.loop = true; Tone.Transport.loopEnd = 1;
Returns this
unsync()
Section titled “unsync()”↳ inherited from Source
unsync(): thisUnsync the source to the Transport.
See also: sync
Returns this
getDefaults()
Section titled “getDefaults()”static
getDefaults(): PlayerOptionsReturns all of the default options belonging to the class.
Returns PlayerOptions
Inherited members
Section titled “Inherited members”- From
ToneWithContext: context, blockTime, sampleTime, get(), immediate(), now(), set(), toFrequency(), toSeconds(), toTicks() - From
Tone: debug, version, disposed, toString() - From
ToneAudioNode: channelCount, channelCountMode, channelInterpretation, numberOfInputs, numberOfOutputs, chain(), connect(), disconnect(), fan(), toDestination(), toMaster()