WebRTC暂停和恢复流 [英] WebRTC pause and resume stream

查看:2350
本文介绍了WebRTC暂停和恢复流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用WebRTC构建一个Web应用程序,当某些事件触发时,需要暂停/恢复视频/音频流。我已经尝试了 getTracks()[0] .stop()但我不知道如何恢复流。

I am trying to use WebRTC to build a web application that needs to pause/resume the video/audio stream when some events trigger. I have tried the getTracks()[0].stop() but I don't know how to resume the stream.

推荐答案

getTracks()[0] .stop()是永久性的。

使用 getTracks()[0] .enabled = false 。要取消暂停 getTracks()[0] .enabled = true

这将用黑色代替你的视频,沉默的音频。

This will replace your video with black, and your audio with silence.

试一试(使用 https小提琴

Try it (use https fiddle for Chrome):

var pc1 = new RTCPeerConnection(), pc2 = new RTCPeerConnection();

navigator.mediaDevices.getUserMedia({ video: true, audio: true })
  .then(stream => pc1.addStream(video1.srcObject = stream))
  .catch(log);

var mute = () => video1.srcObject.getTracks().forEach(t => t.enabled = !t.enabled);

var add = (pc, can) => can && pc.addIceCandidate(can).catch(log);
pc1.onicecandidate = e => add(pc2, e.candidate);
pc2.onicecandidate = e => add(pc1, e.candidate);

pc2.onaddstream = e => video2.srcObject = e.stream;
pc1.onnegotiationneeded = e =>
  pc1.createOffer().then(d => pc1.setLocalDescription(d))
  .then(() => pc2.setRemoteDescription(pc1.localDescription))
  .then(() => pc2.createAnswer()).then(d => pc2.setLocalDescription(d))
  .then(() => pc1.setRemoteDescription(pc2.localDescription))
  .catch(log);

var log = msg => div.innerHTML += "<br>" + msg;

<video id="video1" height="120" width="160" autoplay muted></video>
<video id="video2" height="120" width="160" autoplay></video><br>
<input type="checkbox" onclick="mute()">mute</input><div id="div"></div>
<script src="https://webrtc.github.io/adapter/adapter-latest.js"></script>

PeerConnections基本上停止在此静音状态下发送数据包,因此它非常高效。

PeerConnections basically stop sending packets in this muted state, so it is highly efficient.

这篇关于WebRTC暂停和恢复流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆