2017-05-09 58 views
1

我試圖讓我的用戶減少帶寬使用,如果他們的呼叫通過禁用視頻波濤洶涌。該文件說:Twilio可編程視頻 - 在LocalVideoTrack上調用.disable()不會阻止它被傳輸

「靜音或暫停單一媒體軌道

要控制靜音/取消靜音或LocalVideoTrack單LocalAudioTrack的暫停/取消暫停狀態,您可以使用LocalTrack#啓用和LocalTrack#禁用方法 「

但是,當我使用這個,本地媒體元素變黑(即它停止渲染),但遠程流(打開在不同的窗口)仍然收到視頻。我正在使用的代碼如下。

createLocalVideoTrack().then(track => { 
      var localMediaContainer = document.getElementById(self.local_vid_id); 
      var title = document.createElement('span') 
      title.innerText = "Me"; 
      localMediaContainer.appendChild(title); 
      var videoIcon = document.createElement('span') 
      videoIcon.className = 'glyphicon glyphicon-facetime-video'; 
      videoIcon.title = 'Disable Video'; 
      videoIcon.videoTrack = track; 
      videoIcon.onclick = (event) => { 
       if (event.target.videoTrack.isEnabled) { 
        event.target.videoTrack.disable(); 
        event.target.title = 'Enable Video'; 
       } else { 
        event.target.videoTrack.enable(); 
        event.target.title = 'Disable Video'; 
       } 
      } 
      localMediaContainer.appendChild(videoIcon); 
      localMediaContainer.appendChild(track.attach()); 
     }); 

有沒有其他人遇到過這個問題,有沒有簡單的解決方法?

+0

您好創建的軌道,你不應該依然能夠看到遙控器上的流視頻。您能否與[Twilio支持](https://www.twilio.com/help/contact)聯繫並描述此問題,以便視頻小組可以對其進行調查?謝謝。 – philnash

+0

@philnash我設法找到記錄的方式來做到這一點是通過調用'removeTrack'。沒有任何方法可以創建新的視頻軌道(重新啓用它),而無需重新連接到房間,所以我認爲這可能是與團隊討論的問題。 – thequickbrownfox

+0

嘿,是的,刪除軌道完全從連接中刪除軌道,並需要完全重新連接才能重新開始。禁用它不應該導致這種成本,但如果它不工作,這是沒有用的!我讓團隊意識到了這個問題。謝謝 – philnash

回答

0

在這裏回答我自己的問題,但希望其他人會發現它有用。

您需要移除videoTrack以停止發送。 我使用的代碼的最終版本是

videoIcon.onclick = function(event) { 
    if (event.target.videoTrack){ 
    self.room.localParticipant.videoTracks.forEach((track) => { 
     self.room.localParticipant.removeTrack(track,true); 
    }) 
    trackRemoved(event.target.videoTrack); 
    event.target.videoTrack.stop(); 
    event.target.title = 'Enable Video'; 
    event.target.videoTrack = undefined; 
    } else { 
    // Totally reconnect to the room 
    self.stop(); 
    self.reconnect(); 
    self.startPreview(); 
    } 
} 
0

的潛在的問題是最有可能調用connect(token, options)功能,當你不使用新創建的本地軌道。因此,當您在options中未指定tracks時,它將使用不同的ID創建新的本地曲目。因此,您通過createLocalVideoTrack()createLocalTracks()功能創建的本地視頻軌道看到本地視頻,並通過在connect()功能期間創建的完全不同的本地視頻軌道將視頻數據發送給遠程參與者。

因此,爲了克服這個問題,您應該在選項中指定創建的曲目,以便使用相同的曲目或獲取從connect()函數創建的曲目。之後,如果您撥打disable()功能,它將在通話的雙方靜音。

選項1 - 指定曲目。

const { connect, createLocalTracks } = require('twilio-video'); 

createLocalTracks({ 
    audio: true, 
    video: true 
}).then(localTracks => { 
    return connect('$TOKEN', { 
    name: 'my-room-name', 
    tracks: localTracks 
    }); 
}).then(room => { 
    console.log('Connected to Room:', room.name); 
    localTracks.forEach((track)=>{ 
    // hide camera after 5 seconds 
    if(track.kind === 'video'){ 
     setTimeout(()=>{ 
     track.disable(); 
     } ,5000) 
    } 
    }) 
}); 

選項1 - 使用連接

Twilio.Video.connect('$TOKEN', {name:'my-new-room'}).then(function(room) { 
    console.log('Successfully joined a Room: ', room); 
    room.localParticipant.tracks.forEach((track)=>{ 
    // hide camera after 5 seconds 
    if(track.kind === 'video'){ 
     setTimeout(()=>{ 
     track.disable(); 
     } ,5000) 
    } 
    }); 
    room.on('participantConnected', function(participant) { 
    console.log('A remote Participant connected: ', participant); 
    }) 
}, function(error) { 
    console.error('Unable to connect to Room: ' + error.message); 
}); 
相關問題