视频通话 | 语音通话 |
![]() | ![]() |
npm install -g @vue/cli
vue create call-demo
cd call-demo
npm install @tencentcloud/call-uikit-vue
npm install @tencentcloud/call-uikit-vue2
<template><div><button @click="init()"> init </button><button @click="call()"> start video call </button><details><summary>auto accept feature</summary><button @click="accept()"> accept </button><button @click="reject()"> reject </button><button @click="hangup()"> hangup </button></details><div style="width: 50rem; height: 35rem; border: 1px solid salmon;"><TUICallKit:beforeCalling="beforeCalling":afterCalling="afterCalling":onMinimized="onMinimized":allowedMinimized="true":allowedFullScreen="true":videoDisplayMode="VideoDisplayMode.CONTAIN":videoResolution="VideoResolution.RESOLUTION_1080P" /></div></div></template><script lang="ts" setup>import { TUICallKit, TUICallKitServer, TUICallType, VideoDisplayMode, VideoResolution } from "@tencentcloud/call-uikit-vue";import TIM from "@tencentcloud/chat"// The following 5 variables need to be modified before runningconst SDKAppID = 0;const userID = "";const userSig = "";const callUserID = ""; // callUserID of the called party in the one-to-one call// If you do not want to experience group calls, you do not need to set this parameterlet userIDList = ["xxx", "xxx"]; // UserID of a group call memberasync function init() {try {await TUICallKitServer.init({ SDKAppID, userID, userSig });// await TUICallKitServer.init({ tim, SDKAppID, userID, userSig});alert("init finished");} catch (error: any) {alert(`init failed, reason:${error}`);}}async function call() {try {await TUICallKitServer.call({ userID: callUserID, type: TUICallType.VIDEO_CALL }); // one-to-one call// If you want to experience a group call, comment the call, and then uncomment the following two pieces of code.// const groupID = await createGroupID();// TUICallKitServer.groupCall({ userIDList, groupID, type: 2 }); // group call} catch (error: any) {alert(`call failed, reason:${error}`);}}// chat SDK document: https://web.sdk.qcloud.com/im/doc/en/SDK.html#createGroupasync function createGroupID() {let tim = TIM.create({ SDKAppID });let memberList: any[] = [];userIDList.forEach((user: string) => {memberList.push({ userID: user });});let res = await tim.createGroup({type: TIM.TYPES.GRP_PUBLIC,name: 'WebSDK',memberList});return res.data.group.groupID;}function beforeCalling(type: string, error: any) {console.log("[Callkit Demo] beforeCalling:", type, error);}function afterCalling() {console.log("[Callkit Demo] afterCalling");}function onMinimized(oldStatus: string, newStatus: string) {console.log("[Callkit Demo] onMinimized: " + oldStatus + " -> " + newStatus);}async function accept() {try {await TUICallKitServer.accept();alert("auto-accept");} catch (error) {alert(`auto-accept failed, reason:${error}`);}}async function reject() {try {await TUICallKitServer.reject();alert("auto-reject");} catch (error) {alert(`auto-reject failed, reason:${error}`);}}async function hangup() {try {await TUICallKitServer.hangup();alert("auto-hangup");} catch (error) {alert(`auto-hangup failed, reason:${error}`);}}</script>
<template><div><button @click="init()"> 步骤 1: 初始化 </button><button @click="call()"> 步骤 2: 发起视频通话 </button><div style="width: 50rem; height: 35rem; border: 1px solid salmon;"><TUICallKit /> <!-- 组件默认显示空内容,进入通话后自动显示 --></div></div></template><script>import { TUICallKit, TUICallKitServer, TUICallType } from "@tencentcloud/call-uikit-vue2";export default {name: 'App',data() {return {SDKAppID: 0,userID: '',userSig: '',callUserID: '',};},components: {TUICallKit},methods: {async init() {try {await TUICallKitServer.init({SDKAppID: this.SDKAppID,userID: this.userID,userSig: this.userSig// tim: this.tim // 如果工程中已有 tim 实例,需在此处传入});alert("初始化完成");} catch (error) {alert(`初始化失败,原因:${error}`);}},async call() {try {// 1v1 通话await TUICallKitServer.call({userID: this.callUserID,type: TUICallType.VIDEO_CALL});// 群组通话// TUICallKitServer.groupCall({ userIDList: ["xxx"], groupID: "xxx", type: TUICallType.VIDEO_CALL });} catch (error) {alert(`拨打失败,原因:${error}`);}}},}</script>
import { TUICallKitServer, TUICallType } from "@tencentcloud/call-uikit-vue";await TUICallServer.call({userID: 'mike',type: TUICallType.VIDEO_CALL});
参数 | 类型 | 含义 |
userID | String | 目标用户的 userID: "mike" |
type | Number | 通话的媒体类型,语音通话(type = 1)、视频通话(type = 2) |
import { TUICallKitServer, TUICallType } from "@tencentcloud/call-uikit-vue";await TUICallServer.groupCall({groupID: "xxx",userIDList: ['jack', 'tom'],type: TUICallType.VIDEO_CALL});
参数 | 类型 | 含义 |
groupID | String | 群组 Id,示例: "12345678" |
userIDList | Array<string> | 目标用户的 UserID 列表,示例: ["jane", "mike", "tommy"] |
type | Number | 通话的媒体类型,语音通话(type = 1)、视频通话(type = 2) |
allowedMinimized
属性可以控制浮窗(最小化)的开启。allowedFullScreen
属性可以控制通话窗口全屏的开启。<TUICallKit:allowedMinimized="true":allowedFullScreen="true"/>
filePath
传空即可。try {await TUICallKitServer.setCallingBell(filePath?: string)} catch (error: any) {alert(`Failed to modify the ringtone :${error}`);}
try {await TUICallKitServer.setSelfInfo({ nickName: "xxx", avatar: "http://xxx" });} catch (error: any) {alert(`Failed to setSelfInfo :${error}`);}
beforeCalling
: 通话前会执行afterCalling
: 通话后执行onMinimized
: 组件切换最小化后会执行kickedOut
: 该用户被移出(如重复登录),通话自动结束。(≥v2.3.2 支持)statusChanged
: 通话状态发生了变化,使用方式参照下方示例代码,status 种类参考 API 文档。(≥v2.3.2 支持)allowedMinimized
: 是否允许最小化allowedFullScreen
: 是否允许全屏videoDisplayMode
: 画面显示模式videoResolution
: 通话分辨率kicked-out
: 该用户被移出(如重复登录),通话自动结束status-changed
: 通话状态发生了变化,使用方式参照下方示例代码,status 种类参考 API 文档<script lang="ts" setup>import { TUICallKit, TUICallKitServer, TUICallType, VideoDisplayMode, VideoResolution } from "@tencentcloud/call-uikit-vue";// The following 4 variables need to be modified before running.const SDKAppID = 0;const userID = "";const userSig = "";const callUserID = "";async function init() {try {await TUICallKitServer.init({ SDKAppID, userID, userSig });// await TUICallKitServer.init({ tim, SDKAppID, userID, userSig}); // If there is already a TIM instance in the project, it needs to be passed here.alert("Initialization completed.");} catch (error: any) {alert(`Initialization failed. Reason: ${error}`);}}async function call() {try {await TUICallKitServer.call({ userID: callUserID, type: TUICallType.VIDEO_CALL }); // 1v1 call// TUICallKitServer.groupCall({ userIDList: ["xxx"], groupID: "xxx", type: 2 }); // Group call} catch (error: any) {alert(Call failed. Reason: ${error});}}function beforeCalling(type: string, error: any) {console.log("[Callkit Demo] beforeCalling:", type, error);}function afterCalling() {console.log("[Callkit Demo] afterCalling");}function onMinimized(oldStatus: string, newStatus: string) {console.log("[Callkit Demo] onMinimized: " + oldStatus + " -> " + newStatus);}function handleKickedOut() {console.log("[Callkit Demo] handleKickedOut");}function handleStatusChanged(args: { oldStatus: string; newStatus: string; }) {const { oldStatus, newStatus } = args;if (newStatus === STATUS.CALLING_C2C_VIDEO) {console.log(`[Callkit Demo] handleStatusChanged: ${oldStatus} -> ${newStatus}`);}}</script><template><div><button @click="init()"> init </button><button @click="call()"> video call </button><div style="width: 50rem; height: 35rem; border: 1px solid salmon;"><TUICallKit:beforeCalling="beforeCalling":afterCalling="afterCalling":onMinimized="onMinimized":allowedMinimized="true":allowedFullScreen="true":videoDisplayMode="VideoDisplayMode.CONTAIN":videoResolution="VideoResolution.RESOLUTION_1080P":kickedOut="handleKickedOut":statusChanged="handleStatusChanged"/></div></div></template>
TUICallKitServer
组件的接口,可以灵活的控制 <TUICallKit/>
组件的状态,实现业务侧的更多需求。accept()
: 接听来电reject()
: 拒绝来电hangup()
: 挂断已接通的电话// 此接口需确保在收到来电邀请后调用(status === STATUS.BE_INVITED),通话状态可以参考 `@status-changed` 的抛出。try {await TUICallKitServer.accept();alert(`已自动接听`);} catch (error: any) {alert(`自动接听失败,原因:${error}`);}
// 与 accept() 相同,具有一样的调用时机限制。try {await TUICallKitServer.reject();alert(`已自动拒绝`);} catch (error: any) {alert(`自动拒绝失败,原因:${error}`);}
// 调用此接口需确保已经在呼叫状态或接通状态(status !== STATUS.BE_INVITED && status !== STATUS.IDLE)。通话状态可以参考 `@status-changed` 的抛出。try {await TUICallKitServer.hangup();alert(`已自动挂断`);} catch (error: any) {alert(`自动挂断失败,原因:${error}`);}
本页内容是否解决了您的问题?