在上一篇开发基础上,已经实现了音视频通话。本文是在此基础上继续完成以下内容
- 关闭/开启音频
- 开启/关闭视频
- 屏幕共享
开启/关闭音频
javaScript
/**
* 静音
*/
mute() {
if (!currentSession) {
layer.msg("请先建立视频通话");
return false;
}
var pc = currentSession.sessionDescriptionHandler.peerConnection;
if (pc.getSenders) {
pc.getSenders().forEach(function (sender) {
if (sender.track.kind === 'audio') {
sender.track.enabled = false
layer.msg('您已开启静音');
}
});
} else {
pc.getLocalStreams().forEach(function (stream) {
stream.getAudioTracks().forEach(function (track) {
if (track.kind === 'audio') {
track.enabled = false;
layer.msg('您已开启静音');
}
});
});
}
}
/**
* 解除静音
*/
unmute() {
if (!currentSession) {
layer.msg("请先建立视频通话");
return false;
}
var pc = currentSession.sessionDescriptionHandler.peerConnection;
if (pc.getSenders) {
pc.getSenders().forEach(function (sender) {
if (sender.track.kind === 'audio') {
sender.track.enabled = true
layer.msg('您已解除静音');
}
});
} else {
pc.getLocalStreams().forEach(function (stream) {
stream.getAudioTracks().forEach(function (track) {
if (track.kind === 'audio') {
track.enabled = true
layer.msg('您已解除静音');
}
});
});
}
}
功能截图稍后补充
开启/关闭视频
javaScript
/**
* 打开视频
*/
openVideo() {
if (!currentSession) {
layer.msg("请先建立视频通话");
return false;
}
var pc = currentSession.sessionDescriptionHandler.peerConnection;
if (pc.getSenders) {
pc.getSenders().forEach(function (sender) {
if (sender.track.kind === 'video') {
sender.track.enabled = true
layer.msg('您已打开视频');
}
});
} else {
pc.getLocalStreams().forEach(function (stream) {
stream.getAudioTracks().forEach(function (track) {
if (track.kind === 'video') {
track.enabled = true
layer.msg('您已打开视频');
}
});
});
}
}
/**
* 关闭视频
*/
closeVideo() {
if (!currentSession) {
layer.msg("请先建立视频通话");
return false;
}
var pc = currentSession.sessionDescriptionHandler.peerConnection;
if (pc.getSenders) {
pc.getSenders().forEach(function (sender) {
if (sender.track.kind === 'video') {
sender.track.enabled = false
layer.msg('您已关闭视频');
}
});
} else {
pc.getLocalStreams().forEach(function (stream) {
stream.getAudioTracks().forEach(function (track) {
if (track.kind === 'video') {
track.enabled = false
layer.msg('您已关闭视频');
}
});
});
}
}
功能截图稍后补充
屏幕共享
javaScript
/**
* 屏幕流
*/
shareScreenStream() {
if (currentSession == null) {
layer.msg("请先建立视频通话");
return false;
}
var pc = currentSession.sessionDescriptionHandler.peerConnection;
const displayMediaStreamConstraints = {
video: {
cursor: "always"
},
audio: true
};
//获取分享窗口流
if (navigator.mediaDevices.getDisplayMedia) {
navigator.mediaDevices.getDisplayMedia(displayMediaStreamConstraints).then(mediaStream => {
localVideo.srcObject = mediaStream
pc.getSenders().forEach(sender => {
if (sender.track.kind == 'video') {
var res = sender.replaceTrack(mediaStream.getVideoTracks()[0])
console.log(res)
}
});
//监听手动点击“停止分享”
mediaStream.getVideoTracks()[0].onended = () => {
layer.msg("桌面共享已关闭,正在切换为摄像头数据,请稍后...")
SCREENSHARDING.switchCamera(pc)
}
}).catch(error => {
console.log("error", error)
layer.msg("媒体设备获取异常")
});
} else {
console.log("navigator.mediaDevices.getDisplayMedia false");
layer.msg("浏览器不不支持")
}
},
/**
* 切换为摄像头数据
*/
switchCamera(pc) {
var constraints = {
audio: {
autoGainControl: true,
// 噪音消除
noiseSuppression: true,
// 设置降噪
echoCancellation: true
},
video: true
}
navigator.mediaDevices.getUserMedia(constraints).then(stream => {
localVideo.srcObject = stream
pc.getSenders().forEach(sender => {
if (sender.track.kind == 'video') {
sender.replaceTrack(stream.getVideoTracks()[0])
}
});
}).catch(error => {
layer.msg('切换摄像头失败');
console.error('切换摄像头失败,失败原因:', error)
});
}
功能截图稍后补充