Skip to main content

AudioPlayer Node

Plays an audio file on its output bus, either decoded into memory or streamed from disk, or an MP3 or AAC stream from an http(s) URL, such as internet radio or an HLS playlist. Playback can loop over the region between startPosition and endPosition, with an optional crossfade at the loop point. Outputs mono or stereo to match the output bus.


Configuration​

audioFilePath​

Type: String

The path of the audio file to open when the node is created, or with stream an http(s) URL. When set, the file is loaded (or streamed if stream is true) and playback starts immediately. Updated when the load or stream action succeeds.

autoPlay​

Type: Boolean

Default: true

Start playing as soon as audioFilePath is opened. Set it to false to load the file and wait for the play action, for example when several players share a mixer and should be triggered one at a time.

isLoopingEnabled​

Type: Boolean

Default: false

When true, playback returns to startPosition on reaching endPosition instead of stopping. Defaults to false.

loopCrossfadeMs​

Type: Float

Default: 0.0

Length in milliseconds of the equal-power crossfade applied at the loop point while looping. Defaults to 0, which loops with a hard splice; values above 50 ms are clamped to 50 ms. The fade overlaps the audio before startPosition, so the loop period stays exactly endPosition - startPosition. It therefore needs at least the fade length of audio before startPosition and a loop region longer than the fade; otherwise the node logs a warning and loops without a crossfade. Only applies to loaded audio; streaming ignores it.

startPosition​

Type: Float

Default: 0.0

The position in seconds where playback starts and where looping returns to. Defaults to 0. Applied to the file opened from audioFilePath and kept for files opened later.

stream​

Type: Boolean

Default: false

When true, audioFilePath is streamed while playing instead of being decoded into memory. Streaming keeps memory use flat regardless of file length, at the cost of continuous disk I/O and slower seeking. From disk only Wav files can be streamed; any other format fails to open and the node stays silent. An http(s) URL plays MP3 and AAC (ADTS) files, live streams and HLS playlists, and reports its progress through state and the stateChanged, metadata and error events. loopCrossfadeMs is ignored while streaming. Defaults to false.

timeShiftSeconds​

Type: Float

Default: 0.0

How many seconds of a live stream are kept while paused, so playback resumes where it stopped rather than at the live edge. Past it the oldest audio is dropped. Defaults to 0, which resumes close to live.


Properties​

Inherits properties from its parents, Node, SwitchboardObject.

bufferedSeconds (Read only)​

Type: Float

Seconds of a streamed URL received but not played yet. For a live stream, how far playback is behind the stream, which grows while paused.

duration (Read only)​

Type: Float

The duration of the loaded audio in seconds. 0 when no audio has been loaded, and for a live stream. For a streamed URL it's estimated from the file size and bitrate.

endPosition​

Type: Float

Default: 0.0

The position in seconds where playback ends and where looping wraps around. 0, the default, plays each file to its end. With looping enabled, playback returns to startPosition when this position is reached; the wrap is sample accurate. Other values are clamped between startPosition and the duration of the loaded audio. Kept across load and stream calls.

isLive (Read only)​

Type: Boolean

Whether a streamed URL is a live stream, such as internet radio, rather than a file. false until the server answers.

isLoopingEnabled​

Type: Boolean

Default: false

When true, playback returns to startPosition on reaching endPosition instead of stopping.

isPlaying (Read only)​

Type: Boolean

Indicates whether the audio is currently playing. Becomes false when playback reaches endPosition with looping disabled.

position​

Type: Float

The current position of the playback head in seconds. Setting it seeks within the region between startPosition and endPosition.

startPosition​

Type: Float

Default: 0.0

The position in seconds where playback starts and where looping returns to. Values outside the loaded audio or past endPosition are clamped, and reading the property back returns the clamped value. Kept across load and stream calls, so it does not need to be set again for every file.

state (Read only)​

Type: String

Where a streamed URL is: connecting, buffering (waiting for enough audio to play), ready, finished (a file played to its end) or error. ready for audio from disk or memory, idle when nothing has been opened.

timeShiftSeconds​

Type: Float

Default: 0.0

How many seconds of a live stream are kept while paused, so playback resumes where it stopped. Past it the oldest audio is dropped. 0 resumes close to live.


Actions​

Inherits actions from its parent, SwitchboardObject.

goLive​

Drops what's buffered of a live stream, so playback continues close to the stream's live edge. Does nothing for other audio.

Parameters​

None.

Return Value​

Boolean — true on success.

load​

Decodes an audio file into memory, replacing the current audio and stopping playback. Call play to start it.

Parameters​

NameTypeDescription
audioFilePathStringThe path of the audio file to load. The codec is chosen from the file extension.

Return Value​

Boolean — true on success.

pause​

Pauses playback, keeping the current position. A live stream keeps buffering up to timeShiftSeconds, so playback resumes where it stopped.

Parameters​

None.

Return Value​

Boolean — true on success.

play​

Starts or resumes playback from the current position; a finished non-looping file restarts from its start position. Does nothing if no audio has been loaded.

Parameters​

None.

Return Value​

Boolean — true on success.

stop​

Stops playback and moves the playback head back to startPosition.

Parameters​

None.

Return Value​

Boolean — true on success.

stream​

Opens an audio file to be streamed from disk while playing, or an http(s) URL, replacing the current audio and stopping playback. Unlike load, the file is not read into memory, so seeking is slower. From disk only Wav files can be streamed. A URL plays MP3 and AAC (ADTS) files, live streams and HLS playlists; it connects in the background, so failing to play it is reported by the error event. loopCrossfadeMs is ignored.

Parameters​

NameTypeDescription
audioFilePathStringThe path of the Wav file, or the http(s) URL, to stream.

Return Value​

None.

unload​

Stops playback and releases the audio: frees loaded audio and closes a streamed URL's connection. state becomes idle, and play does nothing until load or stream.

Parameters​

None.

Return Value​

Boolean — true on success.


Events​

error​

Emitted when a streamed URL can't be played: the server refused it, its format or codec isn't supported, or it couldn't be reached. state becomes error.

Data​

NameTypeDescription
messageStringWhat went wrong.

metadata​

Emitted when a streamed URL says what's playing: the station when it connects, and the title when the song or show changes. Titles come with the audio they belong to, a moment before it plays.

Data​

NameTypeDescription
genreStringThe station's genre (icy-genre).
stationNameStringThe station's name (icy-name).
titleStringWhat's playing, usually "Artist - Title". Empty when the stream doesn't say.
urlStringThe station's website (icy-url).

stateChanged​

Emitted when the state of a streamed URL changes.

Data​

NameTypeDescription
stateStringThe new state.

Streaming URLs​

stream with an http(s) URL plays MP3 and AAC (ADTS) — AAC LC, HE-AAC and HE-AAC v2 — from a file, a live stream or an HLS playlist. AAC decodes with AudioToolbox on Apple platforms and FDK-AAC on Linux; other platforms play MP3 only. Ogg, and PLS playlists of stream URLs, aren't supported.

  • A live stream reconnects when the connection drops. A file plays to its end and seeks with range requests, when the server takes them.
  • Pausing a live stream keeps buffering up to timeShiftSeconds, so playing again resumes where it stopped. stop and goLive jump back to the live edge. The connection stays open until unload, or until another file or URL replaces it.
  • Shoutcast/Icecast metadata (the station's name and the current title) arrives as metadata events.

HLS​

An .m3u8 playlist plays live or recorded, from packed audio or MPEG-TS segments.

  • From a master playlist, the highest-bandwidth stream that can be decoded plays, audio-only streams first, or the audio rendition a stream names. When its playlist or segments keep failing, the next one takes over: a copy on another server, or a lower bitrate. The bitrate doesn't otherwise change during playback.
  • Once it has played, a live stream rides out a lost connection, trying its streams in turn until the playlist loads again. When the server keeps refusing its segments, as with an expired token, it fails with an error event.
  • A live stream starts three segments from the end of its playlist, as HLS players do, so it plays a few segment lengths behind the broadcast. It keeps at least one and a half segments buffered, which is also how far behind goLive lands, since new audio arrives a segment at a time.
  • A recording plays to its end and seeks to any position, skipping a segment that can't be fetched. It fails with an error event when the server keeps refusing its segments, or when none of them can be fetched. A live stream whose playlist ends plays on to its end.
  • Titles in the segments' ID3 tags arrive as metadata events.
  • Encrypted playlists, fMP4 segments and byte-range segments aren't supported; the stream fails with an error event that says which.