【问题标题】:VideoCalling using Twilio使用 Twilio 进行视频通话
【发布时间】:2017-08-23 08:40:26
【问题描述】:

我使用 Twilio Javascript sdk 和 php 实现了 twilio 视频通话功能。如果我在 github 上的快速入门教程中使用旧的 sdk 和代码,一切正常。(虽然它不支持 IOS)。当我使用最新的 sdk 时,我遇到了一个错误,并且该功能不再起作用。

index.html 文件中使用的旧 Twilio Javascript sdk <script src="//media.twiliocdn.com/sdk/js/video/releases/1.0.0-beta2/twilio-video.js"></script>

使用了新的 Twilio Javascript sdk: <script src="//media.twiliocdn.com/sdk/js/video/v1/twilio-video.min.js"></script>

这是我在使用最新版本时在控制台中看到的错误。

我的index.js 文件。

var videoClient;
var activeRoom;
var previewMedia;
var identity;
var roomName;

// Check for WebRTC
if (!navigator.webkitGetUserMedia && !navigator.mozGetUserMedia) {
  alert('WebRTC is not available in your browser.');
}

// When we are about to transition away from this page, disconnect
// from the room, if joined.
window.addEventListener('beforeunload', leaveRoomIfJoined);
$(document).ready(function() {
  console.log('testing');

$.getJSON('./token.php', function (data) {
  identity = data.identity;

  console.log(data);
  // Create a Video Client and connect to Twilio
  videoClient = new Twilio.Video.Client(data.token);
  document.getElementById('room-controls').style.display = 'block';

  // Bind button to join room
  document.getElementById('button-join').onclick = function () {
    roomName = document.getElementById('room-name').value;
    if (roomName) {
      log("Joining room '" + roomName + "'...");

      videoClient.connect({ to: roomName}).then(roomJoined,
        function(error) {
          log('Could not connect to Twilio: ' + error.message);
        });
    } else {
      alert('Please enter a room name.');
    }
  };

  // Bind button to leave room
  document.getElementById('button-leave').onclick = function () {
    log('Leaving room...');
    activeRoom.disconnect();
  };
});

});

// Successfully connected!
function roomJoined(room) {
  activeRoom = room;

  log("Joined as '" + identity + "'");
  document.getElementById('button-join').style.display = 'none';
  document.getElementById('button-leave').style.display = 'inline';

  // Draw local video, if not already previewing
  if (!previewMedia) {
    room.localParticipant.media.attach('#local-media');
  }

  room.participants.forEach(function(participant) {
    log("Already in Room: '" + participant.identity + "'");
    participant.media.attach('#remote-media');
  });

  // When a participant joins, draw their video on screen
  room.on('participantConnected', function (participant) {
    log("Joining: '" + participant.identity + "'");
    participant.media.attach('#remote-media');
  });

  // When a participant disconnects, note in log
  room.on('participantDisconnected', function (participant) {
    log("Participant '" + participant.identity + "' left the room");
    participant.media.detach();
  });

  // When we are disconnected, stop capturing local video
  // Also remove media for all remote participants
  room.on('disconnected', function () {
    log('Left');
    room.localParticipant.media.detach();
    room.participants.forEach(function(participant) {
      participant.media.detach();
    });
    activeRoom = null;
    document.getElementById('button-join').style.display = 'inline';
    document.getElementById('button-leave').style.display = 'none';
  });
}

//  Local video preview
document.getElementById('button-preview').onclick = function () {
  if (!previewMedia) {
    previewMedia = new Twilio.Video.LocalMedia();
    Twilio.Video.getUserMedia().then(
    function (mediaStream) {
      previewMedia.addStream(mediaStream);
      previewMedia.attach('#local-media');
    },
    function (error) {
      console.error('Unable to access local media', error);
      log('Unable to access Camera and Microphone');
    });
  };
};

// Activity log
function log(message) {
  var logDiv = document.getElementById('log');
  logDiv.innerHTML += '<p>&gt;&nbsp;' + message + '</p>';
  logDiv.scrollTop = logDiv.scrollHeight;
}

function leaveRoomIfJoined() {
  if (activeRoom) {
    activeRoom.disconnect();
  }
}

他们github 中的代码(使用快速入门示例作为参考)最近发生了更改,他们使用的是 express.js,因此我不确定哪里出了问题。有人可以帮我解决这个问题吗?

【问题讨论】:

  • 你应该使用 Twilio.Video.connect(token, { name: 'room-name' })

标签: javascript php video twilio twilio-php


【解决方案1】:

这里是 Twilio 开发者宣传员。

在测试版和发布版本之间,我们更改了初始化库的方式。您不再使用构造函数,这就是您看到该错误的原因。

相反,您应该使用Twilio.Video.connect(token, { name: 'room-name' }),它会返回一个承诺,一旦您加入房间,该承诺就会解决。

查看latest Twilio Video JS documentation 以获取完整的代码示例并了解如何使用版本 1 SDK。

让我知道这是否有帮助。

【讨论】:

  • 感谢您的帮助。我实现了代码,就像它在您共享的链接中一样,问题已解决。但是后来我遇到了一个新问题,因为我没有看到任何输入框来输入房间名称或连接按钮。控制台中也没有任何错误,例如this。我把code 上传到了github,分享给大家。你能检查一下,让我知道我在哪里出错了。 TIA
  • 我不确定。我会通过记录document.getElementById('room-controls') 发生的情况来开始调试,因为如果这不起作用,您的输入将不会出现。
猜你喜欢
  • 2021-11-28
  • 1970-01-01
  • 2020-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-11
  • 1970-01-01
  • 2011-01-28
相关资源
最近更新 更多