@trtc/call-engine-lite-js 是基于腾讯云 即时通信 IM 和 实时音视频 TRTC 两项付费 PaaS 服务构建出的音视频通信组件,支持双人和多人场景下的音视频通话。
Methods
(static) createInstance(options)
创建 TUICallEngine 实例
Example
let options = {
SDKAppID: 0, // 接入时需要将0替换为您的云通信应用的 SDKAppID
// tim 参数适用于业务中已存在 TIM 实例,需要传入保证 TIM 实例唯一性
tim: tim
};
let tuiCallEngine = TUICallEngine.createInstance(options);
Parameters:
| Name | Type | Description | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
options |
Object |
required
配置 Properties
|
(async) destroyInstance() → {Promise}
销毁 TUICallEngine 实例
- 注:SDK 会先 logout,然后释放资源
Example
try {
await tuiCallEngine.destroyInstance();
// success
} catch (error) {
console.warn('destroyInstance error:', error);
}
Returns:
- Type
- Promise
(async) login(params) → {Promise}
登录接口,所有功能需要先进行登录后才能使用
Example
try {
await tuiCallEngine.login({userID: 'your userID', userSig: 'your userSig'});
// success
} catch (error) {
console.warn('login error:', error);
}
Parameters:
| Name | Type | Description | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
params |
Object |
required
登录配置 Properties
|
Returns:
- Type
- Promise
(async) logout() → {Promise}
登出接口,登出后无法再进行拨打操作
Example
try {
await tuiCallEngine.logout();
// success
} catch (error) {
console.warn('logout error:', error);
}
Returns:
- Type
- Promise
on(eventName, callback, context)
监听事件
Example
let onError = function(error) {
console.log(error);
};
tuiCallEngine.on(TUICallEvent.ERROR, onError, this);
Parameters:
| Name | Type | Description |
|---|---|---|
eventName |
String |
required
事件名 |
callback |
function |
required
事件响应回调 |
context |
Any |
required
期望 callback 执行时的上下文 |
off(eventName, callback, context)
取消监听事件
Example
let onError = function(error) {
console.log(error)
};
tuiCallEngine.off(TUICallEvent.ERROR, onError, this);
Parameters:
| Name | Type | Description |
|---|---|---|
eventName |
String |
required
事件名 |
callback |
function |
required
事件响应回调 |
context |
Any |
required
期望 callback 执行时的上下文 |
(async) calls(params) → {Promise}
发起单人或多人通话
注意:离线推送仅适用于终端(Android 或 iOS),Web 和 微信小程序不支持。
Example
try {
await tuiCallEngine.calls({
userIDList: ['user1', 'user2'],
type: 1,
});
// success
} catch (error) {
console.warn('calls error:', error);
}
Parameters:
| Name | Type | Description | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
params |
Object |
required
群聊拨打通话配置 Properties
|
Returns:
- Type
- Promise
(async) join(params)
主动加入通话
Example
try {
await tuiCallEngine.join({
callId: 'call_123456'
});
// success
} catch (error) {
console.warn('join error:', error);
}
Parameters:
| Name | Type | Description | ||||||
|---|---|---|---|---|---|---|---|---|
params |
required
Properties
|
(async) accept() → {Promise}
当您作为被邀请方收到 TUICallEvent.ON_CALL_RECEIVED 事件的回调时,可以调用该接口接听来电
Example
tuiCallEngine.on(TUICallEvent.ON_CALL_RECEIVED, async () => {
try {
await tuiCallEngine.accept();
// success
} catch (error) {
console.warn('accept error:', error);
}
});
Returns:
- Type
- Promise
(async) reject() → {Promise}
当您作为被邀请方收到 TUICallEvent.INVITED 事件的回调时,可以调用该接口拒绝来电
Example
tuiCallEngine.on(TUICallEvent.INVITED, async () => {
try {
await tuiCallEngine.reject();
// success
} catch (error) {
console.warn('reject error:', error);
}
});
Returns:
- Type
- Promise
(async) hangup() → {Promise}
挂断当前通话,当您处于通话中,可以调用该函数结束通话;当您处于呼叫状态时,可以调用该函数取消通话
- 当您处于通话中,可以调用该接口结束通话
- 当未拨通时, 可用来取消通话
Example
try {
await tuiCallEngine.hangup();
// success
} catch (error) {
console.warn('hangup error:', error);
}
Returns:
- Type
- Promise
(async) openCamera(videoViewDomID, isFrontCameraopt) → {Promise}
您可以调用该接口开启摄像头,处于通话中的用户会收到 USER_VIDEO_AVAILABLE 回调
Example
// 'localView' is the id value corresponding to the div tag
try {
await tuiCallEngine.openCamera('localView');
// success
} catch (error) {
console.warn('openCamera error:', error);
}
Parameters:
| Name | Type | Description |
|---|---|---|
videoViewDomID |
string |
required
该用户数据将渲染到该 dom id 节点里 |
isFrontCamera |
boolean |
前后置摄像头,true 表示前置摄像头。仅支持移动端上使用 |
Returns:
注意:
- 当传入 string 类型的 videoViewDomID 时,需要确保 videoViewDomID 在 DOM 中,否则调用接口会报错。
- 业务侧可自行调整该 DOM 元素的 css 宽高来控制 video 标签显示的宽高。
- Type
- Promise
(async) closeCamera() → {Promise}
关闭摄像头
Example
try {
await tuiCallEngine.closeCamera();
// success
} catch (error) {
console.warn('closeCamera error:', error);
}
Returns:
- Type
- Promise
(async) switchCamera(cameraPosition)
切换前后置摄像头
- 注意: 该接口仅在移动端使用生效。
Example
await tuiCallEngine.switchCamera(TUICallCameraPosition.FRONT); // 切换前置摄像头
await tuiCallEngine.switchCamera(TUICallCameraPosition.BACK); // 切换后置摄像头
Parameters:
| Name | Type | Description |
|---|---|---|
cameraPosition |
TUICallCameraPosition |
required
摄像头类型
|
(async) setVideoQuality(profile) → {Promise}
设置视频分辨率
Example
const profile = '720p';
// Set video quality to 720p
try {
await tuiCallEngine.setVideoQuality(profile);
// success
} catch (error) {
console.warn('setVideoQuality error:', error);
}
Parameters:
| Name | Type | Description | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
profile |
String |
required
视频 profile
|
Returns:
- Type
- Promise
(async) setVideoRenderParams(params)
设置用户视频画面的显示模式
Example
const params = {
userID: '132',
rotation: 0, // 旋转
objectFit: 'contain',
mirror: false,
};
await trtcCloud.setVideoRenderParams(params);
Parameters:
| Name | Type | Description | |||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
params |
IVideoRenderParam |
required
显示参数 Properties
|
(async) openMicrophone() → {Promise}
打开麦克风,处于通话中的用户会收到 USER_AUDIO_AVAILABLE 回调
Example
try {
await tuiCallEngine.openMicrophone();
// success
} catch (error) {
console.warn('openMicrophone error:', error);
}
Returns:
- Type
- Promise
(async) closeMicrophone() → {Promise}
关闭麦克风,处于通话中的用户会收到 USER_AUDIO_AVAILABLE 回调
Example
try {
await tuiCallEngine.closeMicrophone();
// success
} catch (error) {
console.warn('closeMicrophone error:', error);
}
Returns:
- Type
- Promise
(async) startRemoteView(params) → {Promise}
开始渲染远端用户视频流
Example
try {
await tuiCallEngine.startRemoteView({
userID: 'remote_user_123',
videoViewDomID: 'remoteVideo'
});
// success
} catch (error) {
console.warn('startRemoteView error:', error);
}
Parameters:
| Name | Type | Description | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
params |
IStartRemoteViewParam |
required
远端视频渲染参数 Properties
|
Returns:
- Type
- Promise
(async) stopRemoteView(params) → {Promise}
停止远端画面渲染
Example
try {
await tuiCallEngine.stopRemoteView({userID: 'user1'});
// success
} catch (error) {
console.warn('stopRemoteView error:', error);
}
Parameters:
| Name | Type | Description | ||||||
|---|---|---|---|---|---|---|---|---|
params |
Object |
required
渲染远端视频配置 Properties
|
Returns:
- Type
- Promise
(async) setBlurBackground(blurLevel) → {Promise}
开关/设置背景虚化
- 注意: v3.0.5+ 支持,暂不支持 H5,后续支持。
Example
await tuiCallEngine.setBlurBackground(1);
Parameters:
| Name | Type | Description |
|---|---|---|
blurLevel |
number |
required
虚化等级, 0 - 表示关闭背景虚化。目前不支持 blurLevel 等级的设置,后续支持。使用时需要 blurLevel > 0 |
Returns:
- Type
- Promise
(async) setVirtualBackground(imagePath) → {Promise}
开关/设置图片背景虚化
- 注意: v3.0.5+ 支持,暂不支持 H5,后续支持。
Example
await tuiCallEngine.setVirtualBackground('https://xxx.png');
Parameters:
| Name | Type | Description |
|---|---|---|
imagePath |
string |
required
图片路径(可以是 url,也可以是相对路径)。imagePath = '' 表示关闭图片背景虚化 |
Returns:
- Type
- Promise
enableMultiDeviceAbility(enable) → {void}
开启/关闭 TUICallEngine 的多设备登录模式 (尊享版套餐支持)
- 注意:v2.1.1+ 才支持。
Example
tuiCallEngine.enableMultiDeviceAbility(true);
Parameters:
| Name | Type | Description |
|---|---|---|
enable |
boolean |
required
开启、关闭多设备登录. 开启: true; 关闭: false; 默认: 关闭 |
Returns:
- Type
- void
(async) setSelfInfo(options) → {Promise.<void>}
设置当前用户基本信息(用户名、用户头像)
Example
// 设置当前用户用户名及用户头像
await TUICallEngine.setSelfInfo({
nickName: '', // 填写您的新用户名
avatar: '', // 填写您的新头像地址
})
Parameters:
| Name | Type | Description | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
options |
object |
required
Properties
|
Returns:
- Type
- Promise.<void>
(async) setBeautyLevel(style, beautyLevel, whitenessLevel, ruddinessLevel) → {Promise}
支持 webrtc 的基础美颜
Example
await setBeautyLevel({
type: TRTCBeautyStyle.TRTCBeautyStyleSmooth,
beautyLevel: 0.3,
whitenessLevel: 0.4,
ruddinessLevel: 0.5,
})
Parameters:
| Name | Type | Description |
|---|---|---|
style |
TRTCBeautyStyle |
required
磨皮算法,有"光滑"(TRTCBeautyStyleSmooth=0)和"自然"(TRTCBeautyStyleNature=1)两种算法, 默认为 TRTCBeautyStyleNature |
beautyLevel |
number |
required
美颜级别,取值范围 [0 - 9],0表示关闭,1 - 9值越大,效果越明显。 |
whitenessLevel |
number |
required
美白级别,取值范围 [0 - 9],0表示关闭,1 - 9值越大,效果越明显。 |
ruddinessLevel |
number |
required
红润级别,取值范围 [0 - 9],0表示关闭,1 - 9值越大,效果越明显。 |
Returns:
- Type
- Promise
setLogLevel(level)
设置日志级别,低于 level 的日志将不会输出。
Example
tuiCallEngine.setLogLevel(0);
Parameters:
| Name | Type | Description |
|---|---|---|
level |
LOG_LEVEL |
required
日志级别
|
(async) getDeviceList(deviceType)
获取设备列表
Example
try {
const devices = await tuiCallEngine.getDeviceList('camera'); // Get camera list
console.log(devices);
} catch (error) {
console.warn('getDeviceList error:', error);
}
Parameters:
| Name | Type | Description |
|---|---|---|
deviceType |
String |
required
设备类型
|
(async) switchDevice(params)
切换摄像头或麦克风设备
Example
try {
await tuiCallEngine.switchDevice({deviceType: 'camera', deviceId: cameras[0].deviceId}); // Switch device
// success
} catch (error) {
console.warn('switchDevice error:', error);
}
Parameters:
| Name | Type | Description | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
params |
ISwitchDeviceParams |
required
Properties
|