腾讯云-实时音视频-TRTC基础音视频通话接入

基础音视频通话

4.x.x版本的 TRTC Web SDK。
音视频官网

    <div class="personal">
      <!--本地视频流-->
      <div class="video_block">
        <div id="localVideo" class="localVideo"></div>
        <img class="hung_up" src="../../../assets/image/hung_up.png" @click="quitRTC">
      </div>
      <!--远端视频流  多个房间-->
      <div class="add-room" id="remote-video-wrap">
      </div>
    </div>
// 引入
import TRTC from 'trtc-js-sdk';
// 设置日志输入级别
TRTC.Logger.setLogLevel(TRTC.Logger.LogLevel.WARN);

步骤1:创建 Client 对象

通过 TRTC.createClient() 方法创建 Client 对象,参数设置如下:

  • mode: 实时音视频通话模式,设置为videoCall
  • sdkAppId: 您从腾讯云申请的 sdkAppId
  • userId: 用户 ID
  • userSig: 用户签名

**userId 可以获取当前登录用户的账号,也可以随机生成字符串,
通过调用后端接口拿到userSig **

  // 获取userSig
  public async getUserSig (): Promise<void> {
    const res: any = await this.$api.xHttp.get(this.$interfaces.video.usersig, {
      appid: this.sdkappid,
      userid: this.form.userId
    }, null, {defEx: false, defData: false});
    if (res.code === 1) {
      this.userSig = res.data;
    }
  }

-房间ID根据后台返回 加入同一个房间 也可以自己定义房间号-
退出视频要调用一下方法, 彻底关闭本地视频流 否则浏览器会一直提示视频还在调用中

this.localStream.close(); // 彻底关闭本地视频流

代码如下:

  public mounted () {
    this.initTRTC();
  }
 /***
   * 初始化视频
   */
  public initTRTC () {
    this.clientTRTC = TRTC.createClient({
      mode: 'videoCall',
      sdkAppId: this.sdkappid, // 此处填写你的sdkappid
      userId: this.userId,
      userSig: this.userSig
    });
    this.onStreamAdded(); // 监听远端流事件
    this.onStreamSubscribed(); // 订阅远端流
    this.onStreamRemoved(); // 取消订阅远端流
    this.clientJoin();
  }

  // 监听远端流事件
  public onStreamAdded () {
    this.clientTRTC.on('stream-added', (event: any) => {
      const remoteStream = event.stream;
      console.log('远端流增加: ' + remoteStream.getId());
      // 订阅远端流
      this.clientTRTC.subscribe(remoteStream);
    });
  }

  // 订阅远端流
  public onStreamSubscribed () {
    this.clientTRTC.on('stream-subscribed', (event: any) => {
      const remoteStream = event.stream;
      const remoteId = 'remote' + remoteStream.getId();
      console.log('远端流订阅成功:' + remoteStream.getId());
      // 播放远端流
      // remoteStream.play('remoteVideo-' + remoteStream.getId());
      const videoDiv: any = document.createElement('div');
      videoDiv.id = remoteId;
      videoDiv.style.width = '320px';
      videoDiv.style.height = '240px';
      videoDiv.style.marginRight = '20px';
      videoDiv.style.marginBottom = '20px';
      videoDiv.className = 'remoteVidwo';
      (document.getElementById('remote-video-wrap') as any).appendChild(videoDiv);
      remoteStream.play(remoteId);
    });
  }

  // 加入房间
  public clientJoin () {
    this.clientTRTC.join({roomId: this.roomId})
      .catch((error: any) => {
        console.error('进房失败 ' + error);
      })
      .then(() => {
        console.log('进房成功');
        // 创建本地流
        this.localStream = TRTC.createStream({userId: this.userId, audio: true, video: true});
        // 初始化本地流成功
        this.createStream();
        this.enterRoomInfo(); // 告诉后台执行混流
      });
  }

  // 初始化本地流成功
  public createStream () {
    this.localStream
      .initialize()
      .catch((error: any) => {
        console.error('初始化本地流失败 ' + error);
      })
      .then(() => {
        console.log('初始化本地流成功');
        this.localStream.play('localVideo');
        // 发布本地流
        this.pubLocalStream();
      });
  }

  // 本地流发布
  public pubLocalStream () {
    this.clientTRTC
      .publish(this.localStream)
      .catch((error: any) => {
        console.error('本地流发布失败 ' + error);
      })
      .then(() => {
        console.log('本地流发布成功');
      });
  }

  // 取消订阅远端流
  public onStreamRemoved () {
    this.clientTRTC.on('stream-removed', (event: any) => {
      const remoteStream = event.stream;
      const remoteId = 'remote' + remoteStream.getId();
      remoteStream.stop();
      (document.getElementById('remote-video-wrap') as any).removeChild(document.getElementById(remoteId));
    });
  }

  // 关闭视频
  public quitRTC () {
    this.localStream.close(); // 彻底关闭本地视频流
    // 退房
    this.clientTRTC.leave().then((res: any) => {
      console.log('退房成功', res);
      this.$emit('close');
      this.leaveRoomInfo(); // 告诉后台执行混流
    }).catch((error: any) => {
      // 错误不可恢复,需要刷新页面。
      console.error('退房失败 ' + error);
    });
  }
  
  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 12
    评论
要实现实时音视频通话,需要同时使用live-player和live-pusher组件,并且配合使用TRTC实时音视频SDK。 以下是实现步骤和代码示例: 步骤一:准备工作 1. 在腾讯云官网注册账号并创建云通信应用,获取SDKAppID。 2. 下载并引入TRTC的小程序SDK。 步骤二:初始化SDK 在小程序的App.js中初始化TRTC SDK,代码示例如下: ```javascript const trtcConfig = { SDKAppID: 'your_SDKAppID', // 替换为实际的 SDKAppID }; App({ onLaunch: function () { wx.$trtc = require('./path/to/TRTCSDK.js').createInstance(trtcConfig); } }); ``` 步骤三:实现音视频通话 1. 在小程序页面中创建TRTC实例,并设置事件监听: ```javascript const trtcInstance = getApp().$trtc; Page({ data: { localView: '', remoteView: '', }, onLoad: function () { trtcInstance.on('onLocalView', (e) => { this.setData({ localView: e.view }); }); trtcInstance.on('onRemoteView', (e) => { this.setData({ remoteView: e.view }); }); trtcInstance.on('onUserExit', (e) => { // 处理用户退出房间的逻辑 }); }, joinRoom: function () { const roomId = 'your_room_id'; // 替换为实际的房间ID const userId = 'your_user_id'; // 替换为实际的用户ID trtcInstance.joinRoom(roomId, userId); }, exitRoom: function () { trtcInstance.exitRoom(); } }); ``` 2. 在小程序页面的WXML中使用live-pusher组件推送本地音视频流: ```html <live-pusher url="{{pusherUrl}}"></live-pusher> ``` 3. 在小程序页面的WXML中使用live-player组件播放远程音视频流: ```html <live-player src="{{remoteView}}" bindplay="onPlay"></live-player> ``` 4. 在小程序页面的JS中配置推流地址: ```javascript Page({ data: { pusherUrl: '', }, joinRoom: function () { const roomId = 'your_room_id'; // 替换为实际的房间ID const userId = 'your_user_id'; // 替换为实际的用户ID // 获取推流地址 const pusherUrl = trtcInstance.getPusherUrl(roomId, userId); this.setData({ pusherUrl }); // 加入房间 trtcInstance.joinRoom(roomId, userId); }, }); ``` 在上述代码中,`trtcInstance.getPusherUrl()`方法用于获取推流地址,并将其绑定到live-pusher组件的url属性上。`remoteView`用于绑定remoteView事件,将远程音视频流展示在live-player组件中。 这样,使用live-player和live-pusher组件结合TRTC SDK就可以实现实时音视频通话了。具体的实现细节还需要根据业务需求进行调整和完善。以上仅为简单示例,更详细的代码和功能可以参考腾讯云TRTC小程序SDK的官方文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值