如何使用webRTC共享屏幕
我需要让屏幕共享工作。如果是视频共享,它就可以工作。代码如下:如何使用webRTC共享屏幕,webrtc,Webrtc,我需要让屏幕共享工作。如果是视频共享,它就可以工作。代码如下: public n = navigator as any; ngAfterViewInit(): void { const video = this.myVideo.nativeElement; let peerx: any; this.n.getUserMedia = this.n.getUserMedia || this.n.webkitGetUserMedia || this.n.mozGetUserMedi
public n = navigator as any;
ngAfterViewInit(): void {
const video = this.myVideo.nativeElement;
let peerx: any;
this.n.getUserMedia =
this.n.getUserMedia ||
this.n.webkitGetUserMedia ||
this.n.mozGetUserMedia ||
this.n.msGetUserMedia;
}
this.n.getUserMedia( // this.n.mediaDevices.getDisplayMedia
{
video: {
madatory: {
chromeMediaSource: 'screen',
maxWidth: 1920,
maxHeight: 1080,
minAspectRatio: 1.77
},
}
},
(stream: any) => {
peerx = new SimplePeer({
initiator: location.hash === '#init',
trickle: false,
stream,
});
peerx.on('signal', (data) => {
const strData = JSON.stringify(data);
console.log(strData);
this.targetpeer = data;
});
peerx.on('stream', (streamX: any) => {
if ('srcObject' in video) {
video.srcObject = streamX;
} else {
video.src = window.URL.createObjectURL(streamX);
}
const playPromise = video.play();
if (playPromise !== undefined) {
playPromise
.then((_) => {
video.play();
})
.catch((error) => {
console.log(`Playing was prevented: ${error}`);
});
}
});
如果我将行'this.n.getUserMedia(…')更改为'this.n.mediaDevices.getDisplayMedia(…)',我就不会得到'signal'(我需要粘贴到客户端上才能连接的密钥)。您试图混合多年前需要Chrome扩展与getDisplayMedia的约束样式。那是行不通的
const stream = await navigator.mediaDevices.getDisplayMedia({video: true})
有关标准示例,请参阅。感谢您分享您的知识。