Skip to content

Audio & Video API

Local media publishing (camera/mic/screen share) and remote stream rendering.

API Reference

networkLevel

Current network quality level (real-time, 0-5, lower is better).

Equivalent to state.networkQualityLevel$.get() — a synchronous shorthand.

0 = 未知 / unknown, 1 = 极佳 / excellent, 2 = 良好 / good, 3 = 一般 / fair, 4 = 较差 / poor, 5 = 极差 / bad


bindRemoteView()

Bind a remote stream to a DOM element (the only entry point for remote rendering).

Once bound, rendering persists automatically; the SDK handles camera toggle, mic toggle, and failover.

ParameterTypeDescription
userIdstringRemote user ID
domHTMLElementDOM element to render into
streamType?StreamType = StreamType.CameraStream type (default: Camera)

Returns: ViewBinding

Example:

ts
const camBinding  = classroom.bindRemoteView('teacher_001', videoDom, StreamType.Camera);
const screenBinding = classroom.bindRemoteView('teacher_001', screenDom, StreamType.Screen);

// 组件卸载时解绑 / Unbind on component unmount
classroom.unbindRemoteView(camBinding);
classroom.unbindRemoteView(screenBinding);

enableVolumeEvaluation()

Enable/disable the audio volume evaluation pipeline.

The SDK auto-enables this at 500ms after joinClass() succeeds; call this to change the interval or disable.

业务层若要驱动音量动画 UI,可订阅以下事件之一获取数据:

  • TEvent.LOCAL_AUDIO_STATS / TEvent.REMOTE_AUDIO_STATS — TRTC 推/拉流音频统计(含 audioLevel) To drive a volume animation UI, subscribe to one of the following events for data:
  • TEvent.LOCAL_AUDIO_STATS / TEvent.REMOTE_AUDIO_STATS — TRTC publish/subscribe audio stats (with audioLevel)
  • TEvent.LIVE_VOLUME_UPDATE — Live (大班课) stream volume callback (streamId / volume 0–100)
ParameterTypeDescription
intervalnumberCallback interval in ms; pass 0 to disable

Returns: void

Example:

ts
// 调高上报频率到 200ms / Increase reporting frequency to 200ms
classroom.enableVolumeEvaluation(200);

// 订阅本地音频统计驱动音量条 / Drive volume bar with local audio stats
classroom.on(TEvent.LOCAL_AUDIO_STATS, (stats) => {
  updateLocalVolumeBar(stats.audioLevel ?? 0); // 0~100
});

// 大班课快直播音量 / Live stream volume in Big-class
classroom.on(TEvent.LIVE_VOLUME_UPDATE, ({ streamId, volume }) => {
  updateRemoteVolumeBar(streamId, volume);
});

// 关闭上报(如离开课堂前)/ Disable (e.g., before leaving class)
classroom.enableVolumeEvaluation(0);

muteAllRemoteAudio()

Mute/unmute ALL remote audio globally (local mute only — does not affect others).

ParameterTypeDescription
mutebooleantrue=静音 / true=mute, false=取消静音 / false=unmute

Returns: void


muteRemoteAudio()

Mute/unmute a specific remote user's audio (local mute only).

ParameterTypeDescription
userIdstringTarget user ID
mutebooleantrue=静音 / mute, false=取消静音 / unmute

Returns: void


setAvatar()

Set a virtual avatar (powered by webAR — flagship-tier capability).

仅旗舰版套餐支持(isFeatureAvailable('enableAvatar') === true); Only available on the flagship package (isFeatureAvailable('enableAvatar') === true); once enabled, the local video stream is replaced by motion-capture avatar rendering.

ParameterTypeDescription
paramsTAvatarParamAvatar params (model URL / scale / offset, etc.)

Returns: Promise<TResult<void>>

Example:

ts
if (!classroom.isFeatureAvailable('enableAvatar')) {
  toast.warn('当前套餐不支持虚拟形象');
  return;
}
await classroom.setAvatar({ modelUrl: 'https://cdn/avatar.glb', scale: 1 });

setBeautyParam()

Set beauty effect parameters.

Requires package support (isFeatureAvailable('enableBeauty') === true).

All parameter values range 0–100; 0 disables the corresponding effect. state.beautyEnabled$ becomes true when any value > 0.

ParameterTypeDescription
paramsPartial<TBeautyParam>Common fields: whiten 美白、smooth 磨皮、ruddy 红润、

Returns: Promise<TResult<void>>

Example:

ts
// 一键开启自然美颜 / Apply natural beauty preset
await classroom.setBeautyParam({ whiten: 30, smooth: 30, lift: 20, eye: 20 });

// 关闭美颜(所有参数置 0)/ Disable beauty (set all to 0)
await classroom.setBeautyParam({ whiten: 0, smooth: 0, ruddy: 0, lift: 0, eye: 0, chin: 0 });

// 套餐校验 / Package check
if (!classroom.isFeatureAvailable('enableBeauty')) {
  toast.warn('当前套餐不支持美颜');
  return;
}

setVirtualBackground()

Set the virtual background.

需要套餐支持(与美颜共享 enableBeauty 开关)。在摄像头开启后调用立即生效; Requires package support (shares the enableBeauty toggle with beauty). Takes effect immediately after the camera is on; if the camera is off, params are applied on the next startCamera().

ParameterTypeDescription
paramsTVirtualBackgroundParamVirtual background params

Returns: Promise<TResult<void>>

Example:

ts
await classroom.setVirtualBackground({ type: 'blur' });                              // 模糊背景 / blur
await classroom.setVirtualBackground({ type: 'image', imageUrl: 'https://cdn/bg.jpg' }); // 图片替换 / image
await classroom.setVirtualBackground({ type: 'none' });                              // 关闭 / disable

// 套餐校验 / Package check
if (!classroom.isFeatureAvailable('enableBeauty')) {
  toast.warn('当前套餐不支持虚拟背景');
  return;
}

startCamera()

Start the camera and render the local preview into the given DOM element.

After calling, the SDK captures video and publishes it to remote users while showing a local preview in the specified container.

ParameterTypeDescription
elementHTMLElementDOM element to render local preview into
deviceId?stringOptional: specify a camera device ID (uses system default if omitted)

Returns: Promise<TResult<void>>

Example:

ts
// 基础用法 / Basic usage
const result = await classroom.startCamera(document.getElementById('local-video')!);
if (!result.ok) toast.error(result.message);

// 使用指定摄像头 / Use a specific camera
const cams = await classroom.getCameraList();
if (cams.ok && cams.data.length > 0) {
  await classroom.startCamera(videoDom, cams.data[0].deviceId);
}

startMicrophone()

Start the microphone and begin publishing the local audio stream.

After calling, the SDK captures audio and publishes it — remote users hear it immediately.

ParameterTypeDescription
deviceId?stringOptional: specify a microphone device ID (uses system default if omitted)

Returns: Promise<TResult<void>>

Example:

ts
const result = await classroom.startMicrophone();
if (!result.ok) toast.error(result.message);

// 使用指定麦克风 / Use a specific microphone
const mics = await classroom.getMicrophoneList();
if (mics.ok && mics.data.length > 0) {
  await classroom.startMicrophone(mics.data[0].deviceId);
}

startScreenShare()

Start screen sharing (triggers the browser's native screen-picker dialog).

Only one screen share is allowed at a time; call stopScreenShare() first if already sharing.

If element is provided, the shared screen is also rendered locally in that container for preview.

ParameterTypeDescription
element?HTMLElementOptional: render the local screen share preview in this element

Returns: Promise<TResult<void>>

Example:

ts
// 基础用法 / Basic usage
const result = await classroom.startScreenShare();
if (!result.ok) toast.error(result.message);

// 带本地预览 / With local preview
const previewDom = document.getElementById('screen-preview')!;
await classroom.startScreenShare(previewDom);

stopCamera()

Stop the camera and stop the local video stream.

Returns: Promise<TResult<void>>


stopMicrophone()

Stop the microphone and stop the local audio stream.

Returns: Promise<TResult<void>>


stopScreenShare()

Stop screen sharing.

Returns: Promise<TResult<void>>


unbindRemoteView()

Unbind a remote stream (stops rendering and releases subscription resources).

ParameterTypeDescription
bindingViewBindingbindRemoteView() 返回的 ViewBinding 句柄

Returns: void