实时音视频通话TRTC开发简述

  1. 安装 trtc-js-sdk
    main.js:
    import TRTC from ‘trtc-js-sdk’;
    Vue.prototype.$TRTC=TRTC; // 将TRTC 挂载到vue的原型上
    集成 TRTC 桌面浏览器 SDK并引入该模块挂载到vue的原型上全局使用

  2. 检测浏览器是否兼容 TRTC 桌面浏览器 SDK。
    若当前浏览器不兼容 TRTC 桌面浏览器 SDK,建议引导用户去下载最新版本的 Chrome 浏览器。
    TRTC.checkSystemRequirements
    检测通话设备: T R T C . g e t C a m e r a s ( ) 摄像头, TRTC.getCameras()摄像头, TRTC.getCameras()摄像头,TRTC.getMicrophones()麦克风

  3. 创建客户端实例对象:
    根据用户userId 获取签名 TRTC.createClient创建一个实时音视频通话的客户端实例对象client

this.client = TRTC.createClient({
mode: ‘videoCall’,
sdkAppId,
userId,
userSig }); mode: 实时音视频通话模式,设置为‘videoCall’,互动直播模式,设置为 ‘live’ sdkAppId: 您从腾讯云申请的 sdkAppId userId: 用户 ID,随机生成,一个房间内不允许重复的 userId
userSig: 用户签名,基于后台算法生成,防盗刷

  1. 进入房间 client.join
    我的项目是问诊室 所以会socket.sendSock 告诉Android房间号 导诊员userid 导诊员已经进入频道
  2. 创建本地流 localStream=TRTC.createStream
  3. 本地流初始化localStream.initialize 授权摄像头/麦克风
  4. 播放本地流localStream.play(div id)
  5. 发布本地流client.publish(localStream)
  6. 监听远端流增加-订阅远端流

this.client.on(‘stream-added’, event => {
this.remoteStream = event.stream;
//订阅远端流
this.client.subscribe(this.remoteStream); });

  1. 注册远端流状态变化函数,播放远端流

this.client.on(‘stream-subscribed’, event => {
this.remoteStream = event.stream;
this.id = this.remoteStream.getId();
// 播放远端流
this.remoteStream.play(remoteStream-${this.id}); });

  1. 处理远端用户取消推流或退房

// 远端用户取消发布流
client.on(“stream-removed”, event => {
this.KaTeX parse error: Expected 'EOF', got '}' at position 60: …eRoom(); }̲); //远端用户…message.warning(“对方已经离开”);
this.leaveRoom();
});

leaveRoom(type) { // 取消发布本地流
this.client.unpublish(this.localStream).then(() => {
this.cancelTime(); //清除计时器
// 取消发布本地流
this.client.leave();
this.localStream.stop();
// 关闭本地流,释放摄像头和麦克风访问权限
this.localStream.close();
});

}

你可能感兴趣的:(实时音视频,vue.js,前端)