2012-10-22 19 views
6

我看着這些鏈接OpenTok - 如何手動發佈/取消發佈?

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

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

,但他們是手動publishingunpublishing沒有例子,就是發佈/取消發佈不分別以 'streamCreated'/ 'streamDestroyed' 事件處理程序。

我想這樣做的原因是我有一個按鈕來發布/取消發佈,以便用戶可以隨意做到這一點。

有沒有辦法做到這一點?

回答

2

是的,它非常簡單。看看prepublish的源代碼,看看如何。有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'); 
} 
+2

我的代碼使用.publish()和.unpublish()方法了。問題是,當我在取消發佈後發佈時,它不顯示任何內容。 – arvinsim

+2

我也試過你給的鏈接上的現場演示。試圖取消發佈,然後重新發布。沒有工作。 – arvinsim

+2

不要忘記,當你取消發佈它會破壞它所替換的div時,所以你需要確保它能夠將自己附加到某個東西上。我有類似的問題! –