new TRTCCalling(options) → {Object}
腾讯云 TRTCCalling SDK 接入前,您需要:
在 云通信控制台 中创建一个云通信应用,并取得 SDKAppID
。
在 云通信控制台-基本配置 中,选择创建好的云通信应用,开通腾讯实时音视频服务。
- 从v0.6.0起,需要手动安装依赖 trtc-js-sdk 和 tim-js-sdk 以及 tsignaling
Examples
// 为了减小 trtc-calling-js.js 的体积,避免和接入侧已使用的 trtc-js-sdk 和 tim-js-sdk 以及 tsignaling 发生版本冲突
// trtc-js-sdk 和 tim-js-sdk 以及 tsignaling 不再被打包到 trtc-calling-js.js,在使用前您需要手动安装依赖。
npm i trtc-js-sdk --save
npm i tim-js-sdk --save
npm i tsignaling --save
npm i trtc-calling-js --save
// 如果您通过 script 方式使用 trtc-calling-js,需要按顺序先手动引入 trtc.js
<script src="./trtc.js"></script>
// 接着手动引入 tim-js.js
<script src="./tim-js.js"></script>
// 然后手动引入 tsignaling.js
<script src="./tsignaling.js"></script>
// 最后再手动引入 trtc-calling-js.js
<script src="./trtc-calling-js.js"></script>
let options = {
SDKAppID: 0 // 接入时需要将0替换为您的云通信应用的 SDKAppID
};
let trtcCalling = new TRTCCalling(options);
Parameters:
Name | Type | Description | ||||||
---|---|---|---|---|---|---|---|---|
options |
Object |
配置 Properties
|
Returns:
SDK 实例
- Type
- Object
Methods
setLogLevel(level)
设置日志级别,低于 level 的日志将不会输出。
Example
trtcCalling.setLogLevel(0);
Parameters:
Name | Type | Description |
---|---|---|
level |
Number |
日志级别
|
on(eventName, callback, context)
监听事件
Example
let onError = function(error) {
console.log(error);
};
trtcCalling.on(TRTCCalling.EVENT.ERROR, onError, this);
Parameters:
Name | Type | Description |
---|---|---|
eventName |
事件名 |
|
callback |
事件响应回调 |
|
context |
期望 callback 执行时的上下文 |
off(eventName, callback, context)
取消监听事件
Example
let onError = function(error) {
console.log(error)
};
trtcCalling.off(TRTCCalling.EVENT.ERROR, onError, this);
Parameters:
Name | Type | Description |
---|---|---|
eventName |
事件名 |
|
callback |
事件响应回调 |
|
context |
期望 callback 执行时的上下文 |
login(params) → {Promise}
登录IM接口,所有功能需要先进行登录后才能使用
Example
let promise = trtcCalling.login({userID: 'your userID', userSig: 'your userSig'});
promise.then(() => {
//success
}).catch(error => {
console.warn('login error:', error)
});
Parameters:
Name | Type | Description | |||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
params |
Object |
登录配置 Properties
|
Returns:
- Type
- Promise
logout() → {Promise}
登出接口,登出后无法再进行拨打操作
Example
let promise = trtcCalling.logout();
promise.then(() => {
//success
}).catch(error => {
console.warn('logout error:', error)
});
Returns:
- Type
- Promise
call(params) → {Promise}
C2C邀请通话,被邀请方会收到 EVENT.INVITED 事件 如果当前处于通话中,可以调用该接口以邀请第三方进入通话
Example
let promise = trtcCalling.call({userID: 'user1', type: 1, timeout: 0});
promise.then(() => {
//success
}).catch(error => {
console.warn('call error:', error)
});
Parameters:
Name | Type | Description | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
params |
Object |
拨打通话配置 Properties
|
Returns:
- Type
- Promise
groupCall(params) → {Promise}
IM群组邀请通话,被邀请方会收到 EVENT.INVITED 事件 如果当前处于通话中,可以继续调用该接口继续邀请他人进入通话,同时正在通话的用户会收到 EVENT.GROUP_CALL_INVITEE_LIST_UPDATE 事件
Example
let promise = trtcCalling.groupCall({userIDList: ['user1', 'user2'], type: 1, groupID: '群组 ID'});
promise.then(() => {
//success
}).catch(error => {
console.warn('groupCall error:', error)
});
Parameters:
Name | Type | Description | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
params |
Object |
群聊拨打通话配置 Properties
|
Returns:
- Type
- Promise
accept(params) → {Promise}
当您作为被邀请方收到 EVENT.INVITED 事件的回调时,可以调用该接口接听来电
Example
trtcCalling.on(TrtcCalling.EVENT.INVITED, ({inviteID, sponsor, inviteData}) => {
let promise = trtcCalling.accept({inviteID: '38897dbf-ecd4-4b59-a132-bc31529a2b18', roomID: 1234, callType: 1});
promise.then(() => {
//success
}).catch(error => {
console.warn('accept error:', error);
});
});
Parameters:
Name | Type | Description | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
params |
Object |
群聊拨打通话配置 Properties
|
Returns:
- Type
- Promise
reject(params)
当您作为被邀请方收到 EVENT.INVITED 事件的回调时,可以调用该接口拒绝来电
Example
trtcCalling.on(TrtcCalling.EVENT.INVITED, ({inviteID, sponsor, inviteData}) => {
trtcCalling.reject({inviteID: 'user1', isBusy: true, callType: 1});
});
Parameters:
Name | Type | Description | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
params |
Object |
拒绝通话配置 Properties
|
hangup()
挂断电话
- 当您处于通话中,可以调用该接口结束通话
- 当未拨通时, 可用来取消通话
Example
trtcCalling.hangup();
startRemoteView(params) → {Promise}
当您收到 USER_ENTER 事件回调时,可以调用该接口将远端用户的摄像头数据渲染到指定的dom id节点里
Example
let promise = trtcCalling.startRemoteView({userID: 'user1', videoViewDomID: 'video_1'});
promise.then(() => {
//success
}).catch(error => {
console.warn('startRemoteView error:', error)
});
Parameters:
Name | Type | Description | |||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
params |
Object |
渲染远端视频配置 Properties
|
Returns:
- Type
- Promise
stopRemoteView(params)
远端用户的摄像头关闭,可以将远端用户摄像头数据渲染出来的dom id节点移除
Example
trtcCalling.stopRemoteView({userID: 'user1', videoViewDomID: 'video_1'});
Parameters:
Name | Type | Description | |||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
params |
Object |
渲染远端视频配置 Properties
|
startLocalView(params) → {Promise}
当您收到 USER_ENTER 事件回调时,调用该接口将本地用户的摄像头数据渲染到指定的dom id节点里
Example
let promise = trtcCalling.startLocalView({userID: 'user1', videoViewDomID: 'video_1'});
promise.then(() => {
//success
}).catch(error => {
console.warn('startLocalView error:', error)
});
Parameters:
Name | Type | Description | |||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
params |
Object |
渲染本地视频配置 Properties
|
Returns:
- Type
- Promise
stopLocalView(params)
调用该接口将本地用户的摄像头数据渲染到的dom id节点移除
Example
trtcCalling.stopLocalView({userID: 'user1', videoViewDomID: 'video_1'});
Parameters:
Name | Type | Description | |||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
params |
Object |
渲染本地视频配置 Properties
|
openCamera()
您可以调用该接口开启摄像头 处于通话中的用户会收到 USER_VIDEO_AVAILABLE 回调
Example
trtcCalling.openCamera();
closeCamera()
您可以调用该接口关闭摄像头 处于通话中的用户会收到 USER_VIDEO_AVAILABLE 回调
Example
trtcCalling.closeCamera();
setMicMute(isMute)
是否静音mic 处于通话中的用户会收到 USER_AUDIO_AVAILABLE 回调
Example
trtcCalling.setMicMute(true) // 开启麦克风
Parameters:
Name | Type | Description |
---|---|---|
isMute |
Boolean |
true:麦克风关闭 false:麦克风打开 |