2017-09-12 33 views
0

我想给用户的功能,如果他们希望禁用videotrack。我可以为此使用LocalVideoTrack.disable()吗?如果是的话,你能向我展示一个例子吗?如何根据用户偏好允许停止视频轨道并使用twilio视频重新发送视频轨道?

这里是我的代码:

navigator.mediaDevices.getUserMedia({ 
     audio: true, 
     video: {width: 320} 
    }) 
     .then(function (mediaStream) { 
      var connectOptions = { 
       name: roomName, 
       logLevel: 'off', 
       tracks: mediaStream.getTracks() 
      }; 
      return Video.connect(data.token, connectOptions); 
     }) 
     .then(roomJoined, function (error) { 
      log('Could not connect to Twilio: ' + error.message); 
     }); 


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

// Successfully connected! 
function roomJoined(room) { 
    //To collect the timing data for billing purposes 
    $.post('/classrooms/logs/joinedroom/' + lessonId + '/' + identity, function (data) { 
     console.log(data); 
    }); 
    activeRoom = room; 

    log("Joined" + room); 
    log(new Date().getMinutes()); 

    // Attach LocalParticipant's Tracks, if not already attached. 
    // var previewContainer = document.getElementById('local-media'); 
    // if (!previewContainer.querySelector('video')) { 
    //  attachParticipantTracks(room.localParticipant, previewContainer); 
    // } 

    // Attach the Tracks of the Room's Participants. 
    room.participants.forEach(function (participant) { 
     log("Already in Room: '" + participant.identity + "'"); 
     var previewContainer = document.getElementById('remote-media'); 
     attachParticipantTracks(participant, previewContainer); 
    }); 

    // When a Participant joins the Room, log the event. 
    room.on('participantConnected', function (participant) { 
     console.log(participant); 
     log("Joining: '" + participant.identity + "'"); 
    }); 

    // When a Participant adds a Track, attach it to the DOM. 
    room.on('trackAdded', function (track, participant) { 
     log(participant.identity + " added track: " + track.kind); 
     var previewContainer = document.getElementById('remote-media'); 
     var h = previewContainer.offsetWidth * 0.75 + "px"; 
     if (participant.identity === classroom.teacher._id) { 
      attachTracks([track], previewContainer); 

      previewContainer.style.height = h; 

      // } else { 
      //  if(track.kind == 'audio') { 
      //   console.log(typeof(track.kind)); 
      //   attachTracks([track], previewContainer); 
      //  } 
     } 
    }); 

    // When a Participant removes a Track, detach it from the DOM. 
    room.on('trackRemoved', function (track, participant) { 
     log(participant.identity + " removed track: " + track.kind); 
     detachTracks([track]); 
    }); 

    // When a Participant leaves the Room, detach its Tracks. 
    room.on('participantDisconnected', function (participant) { 
     log("Participant '" + participant.identity + "' left the room"); 
     log(new Date().getMinutes()); 
     detachParticipantTracks(participant); 
    }); 
    $('#toggle-video').click(function(e){ 
     console.log(room.localParticipant.videoTracks); 
     // room.localParticipant.videoTracks.disable(); 
    }); 
    // Once the LocalParticipant leaves the room, detach the Tracks 
    // of all Participants, including that of the LocalParticipant. 
    room.on('disconnected', function() { 
     $.post('/classrooms/logs/leftroom/' + lessonId + '/' + identity, function (data) { 
      detachParticipantTracks(room.localParticipant); 
      room.participants.forEach(detachParticipantTracks); 
      activeRoom = null; 
      // document.getElementById('button-join').style.display = 'inline'; 
      document.getElementById('button-leave').style.display = 'none'; 
     }); 
     log('Left'); 
     log(new Date().getMinutes()); 
     detachParticipantTracks(room.localParticipant); 
     room.participants.forEach(detachParticipantTracks); 
     activeRoom = null; 
     // document.getElementById('button-join').style.display = 'inline'; 
     document.getElementById('button-leave').style.display = 'none'; 
    }); 
} 

所以基本上,当用户clciks拨动视频按钮,我要停止发送视频,如果我是个已经发送或开始发送视频我我不是。我怎样才能得到这个LocalVideoTrack的持有?

回答

1

Twilio开发者传道士在这里。

在这种情况下的确可以使用LocalTrack.disable()。或者,为了更容易,您可以将一个布尔参数传递给LocalTrack.enable([enabled]),该参数暂停或取消暂停轨道。以下是你将如何实现的:

function roomJoined(room) { 
    const localParticipant = room.localParticipant; 
    let videoEnabled = true; 

    $('#toggle-video').click(function(e) { 
    videoEnabled = !videoEnabled; 
    localParticipant.videoTracks.forEach(function(videoTrack) { 
     videoTrack.enable(videoEnabled); 
    }); 
    }) 
} 

让我知道这是否有帮助。

+0

我用了你分享的adn代码,当我点击按钮时,它抛出了这个错误:Uncaught TypeError:localParticipant.videoTracks.values(...)forEach不是函数 – lightbringer

+0

这里是代码'$( 'button-video')。click(function(e){console.log('toggle called'); videoEnabled =!videoEnabled; localParticipant.videoTracks.values()。forEach(function(videoTrack){0} .enable(false); }); });' – lightbringer

+0

道歉,我会尝试重现并得到此更正。 – philnash