WebRTC/getUserMedia:如何正确静音本地视频? [英] WebRTC/getUserMedia: How to properly mute local video?

查看:74
本文介绍了WebRTC/getUserMedia:如何正确静音本地视频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的 WebRTC 应用程序中实现静音本地视频 MediaStreamTrack 的功能.我是这样处理的:

I'm trying to implement the functionality for muting the local video MediaStreamTrack in my WebRTC application. Here's how I'm approaching this:

function muteVideo() {
  if (this._localStream && this._localStream.getVideoTracks().length > 0) {
    this._localStream.getVideoTracks()[0].enabled = false;
  }
}

在 Firefox 中,本地流正确附加到的 元素在静音时呈现黑色.在 Chrome 中,不会渲染黑色,但图片会冻结.但是,在这两种浏览器中,摄像头的绿灯一直亮着,这显然是不受欢迎的行为.(我希望我的用户看到应用程序实际上在视频静音时断开与摄像头的连接.)

In Firefox, the <video> element to which the local stream is attached correctly renders blackness on mute. In Chrome, blackness is not rendered but the picture freezes. However, in both browsers, the camera's green light stays on, which is clearly undesired behavior. (I want my users to see that the application actually disconnects from the camera on video mute.)

如果我执行this._localStream.stop(),相机的灯会熄灭,但随后音频也会熄灭.

The camera's light goes off if I do this._localStream.stop(), but then the audio goes off, too.

媒体捕获规范的当前草案提到了 MediaStreamTrack.stop() 方法,但它目前似乎未在 Chrome 和 Firefox 中实现.

The current draft of the Media Capture spec mentions the MediaStreamTrack.stop() method but it currently seems unimplemented in Chrome and Firefox.

那么有没有办法让本地视频静音:

So is there a way to mute local video while:

  1. 关闭相机的灯
  2. 不会丢失音轨?

推荐答案

今天

track.stop() 在 Firefox 中运行良好.铬落后.结束曲目的规范方式(https fiddle):

Today

track.stop() works just fine in Firefox. Chrome's behind. Spec way to end a track (https fiddle):

navigator.mediaDevices.getUserMedia({video: true, audio: true})
  .then(stream => video.srcObject = stream)
  .catch(e => log(e.name + ": "+ e.message));

let stop = k => video.srcObject.getTracks().map(t => t.kind == k && t.stop());

<video id="video" width="160" height="120" autoplay></video><br>
<button onclick="stop('video')">Stop Video</button>
<button onclick="stop('audio')">Stop Audio</button>

这为您提供了一种在保留音频的同时关闭视频的方法,而无需在 Firefox 中重新提示.重新打开视频时您仍然会收到提示,因此它并不完美,但效果好 50%.

This gives you a way to turn off video while keeping audio, without permission re-prompt in Firefox. You still get prompted when turning video back on, so it's not perfect, but 50% better.

在 Chrome 赶上之前,您的其他答案(每次删除并重新粘贴)应该可以在那里工作,因为它们永远不会重新提示.

Until Chrome catches up, your other answer (drop and re-gUM each time) should work there, since they never re-prompt.

通过浏览器检测并结合这些答案,应该有可能提出在多个浏览器中运行良好的东西,直到浏览器迎头赶上.

By browser-detecting and combining these answers, it should be possible to come up with something that works well in more than one browser, until browsers catch up.

规范最近解决了这个问题允许浏览器在暂时静音期间关闭相机灯(例如 track.enabled == false),前提是相机 access 指示器保持开启:

The spec has recently addressed this by allowing browsers to turn off the camera light during temporal mute (e.g. track.enabled == false), provided a camera access indicator remains on:

鼓励用户代理提供 anyAccessible 当前状态的持续指示.

"The User Agent is encouraged to provide ongoing indication of the current state of anyAccessible.

鼓励用户代理提供 anyLive 当前状态的持续指示,并使任何通用硬件设备指示灯匹配."

The User Agent is encouraged to provide ongoing indication of the current state of anyLive and to make any generic hardware device indicator light match."

规范中的这些声明之前有更强大的语言,使指标成为一项要求.

Stronger language precedes these statements in the spec, making indicators a requirement.

目前,浏览器没有正确实现这一点.Chrome 很接近,在最近访问后右侧的 url 栏中有一个微小的相机访问指示器,但它没有出现在页面加载中以警告在上次访问时授予了持久访问;网站可以随时开启摄像头.

Currently, browsers do not implement this correctly. Chrome is close, with a tiny camera access indicator inside the url bar on the right after recent access, but it fails to appear on page load to warn that persistent access was granted on a previous visit; the site can turn the camera on at any time.

这篇关于WebRTC/getUserMedia:如何正确静音本地视频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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