OpenTok - 如何手动发布/取消发布?

arv*_*sim 7 api videochat video-conferencing tokbox

我查看了这些链接

http://www.tokbox.com/opentok/api/tools/js/documentation/overview/publish.html

http://www.tokbox.com/opentok/api/tools/js/tutorials/overview

但它们不是手动发布发布的示例,也就是说,不分别使用'streamCreated'/'streamDestroyed'事件处理程序发布/取消发布.

我想这样做的原因是我有一个发布/取消发布的按钮,以便用户可以随意进行.

有没有办法做到这一点?

Ric*_*hoe 4

是的,而且非常简单。查看预发布源代码以了解具体操作方法。有 2 个函数 startPublishing() 和 stopPublishing() 可以实现此目的。

它们主要用于session.publish(publisher);发布和session.unpublish(publisher);取消发布。

这是我用来处理的代码:

// Called by a button to start publishing to the session
function startPublishing() {
    if (!publisher) {
        var parentDiv = document.getElementById("myCamera");
        var publisherDiv = document.createElement('div'); // Create a div for the publisher to replace
        publisherDiv.setAttribute('id', 'opentok_publisher');
        parentDiv.appendChild(publisherDiv);
        var publisherProps = {
            width : VIDEO_WIDTH,
            height : VIDEO_HEIGHT
        };
        publisher = TB.initPublisher(apiKey, publisherDiv.id, publisherProps); // Pass the replacement div id and properties
        session.publish(publisher);
        show('unpublishLink');
        hide('publishLink');
    }
}

//Called by a button to stop publishing to the session
function stopPublishing() {
    if (publisher) {
        session.unpublish(publisher);
    }
    publisher = null;

    show('publishLink');
    hide('unpublishLink');
}
Run Code Online (Sandbox Code Playgroud)

  • 我的代码也使用 .publish() 和 .unpublish() 方法。问题是当我取消发布后发布时,它不显示任何内容。 (3认同)
  • 我还尝试了您提供的链接上的实时演示。尝试取消发布然后重新发布。没用。 (3认同)
  • 不要忘记,当您取消发布时,它会破坏它所替换的 div,因此您需要确保它可以再次将其自身附加到某些东西上。我有类似的问题! (3认同)