ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[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); }); ```