AudioStream interface

Common shape for audio streams. Extended by LocalAudioStream (adds device + processor control + silence detection) and RemoteAudioStream (adds setVolume + audioLevel + server-driven state).

Blueprint

Full surface at a glance. Subtypes (Local / Remote / ScreenAudio) add to this. No events on the base.

interface AudioStream { // Identity readonly id: string; readonly codec: string; // "opus" | "g722" | ... // Sync state flags readonly isPlaying: boolean; readonly isMuted: boolean; readonly isEnded: boolean; // Render โ€” escape hatch (SDK auto-plays remote audio by default) attach(el: HTMLAudioElement): void; detach(el: HTMLAudioElement): void; // Stats getStats(): Promise<AudioStats>; // Escape hatch โ€” raw track for advanced use cases getMediaStreamTrack(): MediaStreamTrack; // No events on the base โ€” see subtypes (silent-detected / state-changed / ended) }
Audio rendering is automatic for remote streams. The SDK creates an internal <audio> element and plays remote audio without any attach call. Local audio is not auto-played (feedback prevention). The attach method below is an escape hatch for custom routing.

Properties

id

Server-assigned stream id.

readonly id: string

codec

Audio codec in use.

readonly codec: string // "opus" | "g722" | ...

isPlaying / isMuted / isEnded

Sync booleans reflecting current state.

readonly isPlaying: boolean readonly isMuted: boolean readonly isEnded: boolean

Methods

attach / detach

Render this audio stream into a custom <audio> element. Escape hatch only โ€” by default the SDK auto-plays remote audio in an internal element, and local audio doesn't auto-play. Use this when you need custom output device routing per element, custom playback UI, or a Web Audio integration via the audio element.

Auto-detach on stream end. The SDK tracks every element passed to attach() and clears their srcObject when the stream ends (publisher unpublished, unsubscribed, etc.). You only need detach() for re-routing.

attach(el: HTMLAudioElement): void detach(el: HTMLAudioElement): void
Example โ€” route specific participant to a different output device
const audioEl = document.createElement('audio');
audioEl.autoplay = true;
document.body.appendChild(audioEl);

p.audio.attach(audioEl);
await audioEl.setSinkId(headsetDeviceId);  // route this participant to a headset

getStats async

One-shot stats snapshot โ€” bitrate, packet loss, jitter, codec.

getStats(): Promise<AudioStats>

getMediaStreamTrack escape hatch

Returns the underlying MediaStreamTrack. Same contract as VideoStream.getMediaStreamTrack โ€” read-only / forward-only consumption; do not call lifecycle methods.

getMediaStreamTrack(): MediaStreamTrack
Example โ€” Web Audio analyser for waveform visualization
const track = p.audio.getMediaStreamTrack();
const ctx = new AudioContext();
const source = ctx.createMediaStreamSource(new MediaStream([track]));
const analyser = ctx.createAnalyser();
source.connect(analyser);
// ...drive a canvas waveform from analyser data

Events

The base interface defines no events. Operational events live on the subtypes:

Lifecycle events (audio-published, audio-subscribed, etc.) fire on the participant.

See also: LocalAudioStream RemoteAudioStream VideoStream

Related open questions: Q13 โ€” Stream object abstraction across platforms