🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[TOC] ## 入门 一旦将`RTCPeerConnection`连接到远程对等方,便可以在它们之间流式传输音频和视频 ``` const localStream = await getUserMedia({vide: true, audio: true}); const peerConnection = new RTCPeerConnection(iceConfig); localStream.getTracks().forEach(track => { peerConnection.addTrack(track, localStream); }); ``` ## 添加远程轨道 为了接收其他对等方添加的远程轨道,我们在本地`RTCPeerConnection`上注册了一个侦听器,以监听`track`事件。 ``` const remoteStream = MediaStream(); const remoteVideo = document.querySelector('#remoteVideo'); remoteVideo.srcObject = remoteStream; peerConnection.addEventListener('track', async (event) => { remoteStream.addTrack(event.track, remoteStream); }); ```