Skip to main content

StreamPlayer Node

Plays an MP3 or AAC stream from an http(s) URL, such as internet radio or an HLS playlist. It's a Switchboard.AudioPlayer node that also takes the stream as url, with a load action that streams it; AudioPlayer's stream action does the same. Only available in builds with networking. AAC needs a platform decoder: AudioToolbox on Apple platforms and FDK-AAC on Linux.


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 a stream is opened by url, audioFilePath, or the load or stream action. Set it to false to open a stream and wait for the play action.

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.

url​

Type: String

The URL of a stream to load when the node is created. When set, playback starts immediately unless autoPlay is false.


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​

Loads the stream at the given URL. Plays it when autoPlay is true, the default; otherwise call play to start it.

Parameters​

NameTypeDescription
urlStringThe URL of the stream to load.

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 a Wav file to be streamed from disk, or an http(s) URL, replacing the current audio. Plays it when autoPlay is true, the default; otherwise call play to start it. 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.