OpenTok - How to publish/unpublish manually?

末鹿安然 提交于 2019-12-10 13:03:30

问题


I looked at these links

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

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

but their are no examples for publishingunpublishing manually, that is, publishing/unpublishing without using 'streamCreated'/'streamDestroyed' event handler respectively.

The reason I want to do this is that I have a button to publish/unpublish so that the user can do it at will.

Is there a way to do this?


回答1:


Yes and it is very simple. Check out the prepublish source code to see how. There are 2 functions, startPublishing() and stopPublishing() which achieve this.

Primarily they use session.publish(publisher);to publish and session.unpublish(publisher); to unpublish.

Here is code I have used to work off:

// 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');
}


来源:https://stackoverflow.com/questions/13008078/opentok-how-to-publish-unpublish-manually

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!