[英]How to add audio/video mute/unmute buttons in WebRTC video chat
I'm trying to create WebRTC video chat.我正在尝试创建 WebRTC 视频聊天。 Now I'm stacked on creating media-tracks buttons (mute video to enable or disable video sending, and mute audio to make the same with audio).现在我忙于创建媒体轨道按钮(静音视频以启用或禁用视频发送,静音音频以与音频相同)。 Here is my code.这是我的代码。
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script type="text/javascript" src="http://cdn.peerjs.com/0.3/peer.min.js"></script>
<script>
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
var myStream;
var peer = new Peer({key: 'PeerJS key'});
var setOthersStream = function(stream){
$('#others-video').prop('src', URL.createObjectURL(stream));
};
var setMyStream = function(stream){
myStream = stream;
$('#video').prop('src', URL.createObjectURL(stream));
};
peer.on('open', function(id){
$('#peer-id').text(id);
});
peer.on('call', function(call){
call.answer(myStream);
call.on('stream', setOthersStream);
});
$(function(){
navigator.getUserMedia({audio: true, video: true}, setMyStream, function(){});
$('#call').on('click', function(){
var call = peer.call($('#others-peer-id').val(), myStream);
call.on('stream', setOthersStream);
});
});
peer.on('error', function(e){
console.log(e.message);
});
Can anyone guide me please?谁能指导我吗?
The video and audio tracks in your stream have an enabled
attribute you can modify.流中的视频和音频轨道具有可以修改的已enabled
属性。 Eg:例如:
function muteMic() {
myStream.getAudioTracks().forEach(track => track.enabled = !track.enabled);
}
function muteCam() {
myStream.getVideoTracks().forEach(track => track.enabled = !track.enabled);
}
finally I got it work!终于我得到它的工作! The first answer for question from this " webrtc video stream stop sharing " guided me to the right direction.这个“ webrtc video stream stop sharing ”问题的第一个答案引导我走向了正确的方向。 I created two new functions to mute video and audio, and bound them to appropriate buttons in html file.我创建了两个新函数来使视频和音频静音,并将它们绑定到 html 文件中的适当按钮。 And finally it became look like this:最后它变成了这个样子:
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
var myStream;
var peer = new Peer({key: 'PeerJS key'});
var setOthersStream = function(stream){
$('#others-video').prop('src', URL.createObjectURL(stream));
};
var setMyStream = function(stream){
myStream = stream;
$('#video').prop('src', URL.createObjectURL(stream));
};
peer.on('open', function(id){
$('#peer-id').text(id);
});
peer.on('call', function(call){
call.answer(myStream);
call.on('stream', setOthersStream);
});
$(function(){
navigator.getUserMedia({audio: true, video: true}, setMyStream, function(){});
$('#call').on('click', function(){
var call = peer.call($('#others-peer-id').val(), myStream);
call.on('stream', setOthersStream);
});
});
peer.on('error', function(e){
console.log(e.message);
});
//create button to toggle video
var video_button = document.createElement("video_button");
video_button.appendChild(document.createTextNode("Toggle hold"));
video_button.video_onclick = function(){
myStream.getVideoTracks()[0].enabled = !(myStream.getVideoTracks()[0].enabled);
}
var audio_button = document.createElement("audio_button");
video_button.appendChild(document.createTextNode("Toggle hold"));
audio_button.audio_onclick = function(){
myStream.getAudioTracks()[0].enabled = !(myStream.getAudioTracks()[0].enabled);
}
Hope it will help to someone.希望它会对某人有所帮助。
Use MediaStreamTrack.enabled .使用MediaStreamTrack.enabled 。
let mic_switch = true;
let video_switch = true;
function toggleVideo() {
if(localStream != null && localStream.getVideoTracks().length > 0){
video_switch = !video_switch;
localStream.getVideoTracks()[0].enabled = video_switch;
}
}
function toggleMic() {
if(localStream != null && localStream.getAudioTracks().length > 0){
mic_switch = !mic_switch;
localStream.getAudioTracks()[0].enabled = mic_switch;
}
Same for remote stream also.远程流也一样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.