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
// 从v0.10.2起,新增 tim 参数
// tim 参数适用于业务中已存在 TIM 实例,为保证 TIM 实例唯一性
tim: tim
};
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 |
String |
事件名 |
callback |
function |
事件响应回调 |
context |
Any |
期望 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 |
String |
事件名 |
callback |
function |
事件响应回调 |
context |
Any |
期望 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 事件
- v1.0.0 及其之后版本,取消 timeout 参数
- v1.0.0 及其之后版本,新增 offlinePushInfo 参数 注意:离线推送仅适用于终端(Android 或 iOS),Web 和 微信小程序不支持。
Example
// v1.0.0 之前
let promise = trtcCalling.call({userID: 'user1', type: 1, timeout: 30});
// v1.0.0 版本及其之后
const offlinePushInfo = {
title: '',
description: '您有一个通话请求',
extension: ''
}
let promise = trtcCalling.call({userID: 'user1', type: 1, offlinePushInfo: offlinePushInfo});
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 事件
- v1.0.0 及其之后版本,新增 offlinePushInfo 参数 注意:离线推送仅适用于终端(Android 或 iOS),Web 和 微信小程序不支持。
Example
// v1.0.0之前
let promise = trtcCalling.groupCall({userIDList: ['user1', 'user2'], type: 1, groupID: 'IM群组 ID'});
// v1.0.0及其之后
const offlinePushInfo = {
title: '',
description: '您有一个通话请求',
extension: ''
}
let promise = trtcCalling.groupCall({userIDList: ['user1', 'user2'], type: 1, groupID: 'IM群组 ID', offlinePushInfo: offlinePushInfo});
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 事件的回调时,可以调用该接口接听来电
- v1.0.0 及其之后版本,取消 params 参数
Example
trtcCalling.on(TrtcCalling.EVENT.INVITED, ({inviteID, sponsor, inviteData}) => {
let roomID = inviteData.roomID
// v1.0.0之前
let promise = trtcCalling.accept({inviteID: '38897dbf-ecd4-4b59-a132-bc31529a2b18', roomID: roomID, callType: 1});
// v1.0.0及其之后
let promise = trtcCalling.accept();
promise.then(() => {
//success
}).catch(error => {
console.warn('accept error:', error);
});
});
Parameters:
Name | Type | Description | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
params |
Object |
群聊拨打通话配置 -- 仅限于v1.0.0之前版本 Properties
|
Returns:
- Type
- Promise
reject(params)
当您作为被邀请方收到 EVENT.INVITED 事件的回调时,可以调用该接口拒绝来电
- v1.0.0 及其之后版本,,取消 params 参数
Example
trtcCalling.on(TrtcCalling.EVENT.INVITED, ({inviteID, sponsor, inviteData}) => {
// v1.0.0之前
trtcCalling.reject({inviteID: 'user1', isBusy: true, callType: 1});
// v1.0.0及其以后
trtcCalling.reject();
});
Parameters:
Name | Type | Description | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
params |
Object |
拒绝通话配置 -- 仅限于v1.0.0之前的版本 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节点移除
- v1.0.0 及其之后版本,移除 videoViewDomID 参数
Example
// v1.0.0之前
trtcCalling.stopRemoteView({userID: 'user1', videoViewDomID: 'video_1'});
// v1.0.0及其之后
trtcCalling.stopRemoteView({userID: 'user1'});
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节点移除
- v1.0.0 及其之后版本,移除 videoViewDomID 参数
Example
// v1.0.0之前
trtcCalling.stopLocalView({userID: 'user1', videoViewDomID: 'video_1'});
// v1.0.0及其之后
trtcCalling.stopLocalView({userID: 'user1'});
Parameters:
Name | Type | Description | ||||||
---|---|---|---|---|---|---|---|---|
params |
Object |
渲染本地视频配置 Properties
|
openCamera()
您可以调用该接口开启摄像头 处于通话中的用户会收到 USER_VIDEO_AVAILABLE 回调
Example
trtcCalling.openCamera().then(() => {
//success
}).catch(error => {
console.warn('openCamera error:', error)
});
closeCamera()
您可以调用该接口关闭摄像头 处于通话中的用户会收到 USER_VIDEO_AVAILABLE 回调
Example
trtcCalling.closeCamera().then(() => {
//success
}).catch(error => {
console.warn('closeCamera error:', error)
});
setMicMute(isMute)
是否静音mic 处于通话中的用户会收到 USER_AUDIO_AVAILABLE 回调
Example
trtcCalling.setMicMute(true).then(() => {
//success
}).catch(error => {
console.warn('setMicMute error:', error)
}); // 开启麦克风
Parameters:
Name | Type | Description |
---|---|---|
isMute |
Boolean |
true:麦克风关闭 false:麦克风打开 |
setVideoQuality(profile)
您可以调用此接口设置视频质量
- v0.8.0 及其之后版本,新增该方法
- 此方法需在 call、groupCall、accept 之前设置,之后设置不生效
Example
const profile = '720p';
trtcCalling.setVideoQuality(profile).then(() => {
//success
}).catch(error => {
console.warn('setVideoQuality error:', error)
}); // 设置视频质量为720p
Parameters:
Name | Type | Description | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
profile |
String |
视频 Profile
|
switchToAudioCall()
您可以调用此接口将视频通话切换语音通话
- v0.10.0 及其之后版本,新增该方法
- 仅支持1v1通话过程中使用
- 失败监听 ERROR 事件,code: 60001
Example
let promise = trtcCalling.switchToAudioCall() // 视频通话切换语音通话
promise.then(() => {
//success
}).catch(error => {
console.warn('switchToVideoCall error:', error)
});
switchToVideoCall()
您可以调用此接口将语音通话切换视频通话
- v0.10.0 及其之后版本,新增该方法
- 仅支持1v1通话过程中使用
- 失败监听 ERROR 事件,code: 60002
Example
let promise = trtcCalling.switchToVideoCall() // 语音通话切换视频通话
promise.then(() => {
//success
}).catch(error => {
console.warn('switchToVideoCall error:', error)
});
getCameras()
您可以调用此接口获取摄像头设备列表
- v1.0.0 及其之后版本,新增该方法
Example
trtcCalling.getCameras().then((devices)=>{
console.log(devices)
}) // 获取摄像头列表
getMicrophones()
您可以调用此接口获取麦克风设备列表
- v1.0.0 及其之后版本,新增该方法
Example
trtcCalling.getMicrophones().then((devices)=>{
console.log(devices)
}) // 获取麦克风列表
switchDevice(params)
您可以调用此接口切换摄像头或麦克风设备
- v1.0.0 及其之后版本,新增该方法
Example
let promise = trtcCalling.switchDevice({deviceType: 'video', deviceId: cameras[0].deviceId}) // 切换设备
promise.then(() => {
//success
}).catch(error => {
console.warn('switchDevice error:', error)
});
Parameters:
Name | Type | Description | |||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
params |
ISwitchDeviceParams |
Properties
|
setSelfInfo(params)
设置自己的昵称或头像
Example
let promise = trtcCalling.setSelfInfo({nickName: 'video', avatar:'http(s)://url/to/image.jpg'})
promise.then(() => {
//success
}).catch(error => {
console.warn('setSelfInfo error:', error)
});
Parameters:
Name | Type | Description | |||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
params |
ISwitchDeviceParams |
Properties
|