【问题标题】:How can I stop publishing only the video track/audio track using agora web sdk ng?如何停止使用 agora web sdk ng 仅发布视频轨道/音频轨道?
【发布时间】:2020-11-09 10:27:16
【问题描述】:
async toggleVideoCam() {

    if (this.rtc.localVideoTrack) {
      await this.rtc.client.localVideoTrack.setEnabled(false);
      this.rtc.localVideoTrack.close();
      document.getElementById('localvideo').remove();
    } else {
      await this.rtc.client.localVideoTrack.setEnabled(true);
    
      const playerContainer = document.createElement('div');
      const userIdContainer = document.createElement('span');
      userIdContainer.className = 'user-id-holder';
      playerContainer.className = 'localUserVideo';
      let liveStreamContainer = document.querySelector('.videoTracks-container');

      playerContainer.id = 'localvideo';
      userIdContainer.innerHTML = 'local video';
      playerContainer.append(userIdContainer);

      if (!document.getElementById(playerContainer.id)) {
        liveStreamContainer.append(playerContainer);
        // Play the local audio and video tracks
        // SDK dynamically creates a player in the container for playing the local video track
        this.rtc.localVideoTrack.play(playerContainer);
      }
    }
  }

现在我想切换发布localVideoTrack 我该怎么做? 是的,我在 localVideoTrack 上添加了一个 setEnabled 方法,但我无法读取未定义的 setEnabled 属性。

【问题讨论】:

    标签: agora.io agora-web-sdk-ng


    【解决方案1】:

    您可以简单地使用适当的布尔值在本地轨道上调用setEnabled 方法来切换发布轨道。 https://agoraio-community.github.io/AgoraWebSDK-NG/api/en/interfaces/ilocalvideotrack.html#setenabled

    【讨论】:

    • TypeError: 无法读取未定义的属性“setEnabled”
    • 您能否使用 setEnabled 更新您的代码(在问题中)?
    【解决方案2】:

    第一件事是您必须将 await 与 setenabled 方法一起使用,第二件事是客户端对象中不存在轨道。

      try {
                  if (this.rtc.localVideoTrack) await rtc.localVideoTrack.setEnabled(true);  // This is used to publish camera
              } catch (error) {
                console.log(error, 'error');
              }
            }
            else {
              try {
                if (this.rtc.localVideoTrack) await rtc.localVideoTrack.setEnabled(false); // With this you can close camera
              } catch (error) {
                console.log(error, 'error');
              }
            }
    

    【讨论】:

      【解决方案3】:

      在您的情况下,您可以将最后一个用于音频,将第一个用于视频:

      this.rtc.localVideoTrack.setEnabled(boolean)
      
      this.rtc.localAudioTrack.setEnabled(boolean)
      

      【讨论】:

        猜你喜欢
        • 2020-12-27
        • 1970-01-01
        • 2014-10-16
        • 2011-10-07
        • 1970-01-01
        • 2012-10-02
        • 1970-01-01
        • 1970-01-01
        • 2015-10-19
        相关资源
        最近更新 更多