在 getUserMedia 中将视频流设为可选

编程入门 行业动态 更新时间:2024-10-28 03:21:14
本文介绍了在 getUserMedia 中将视频流设为可选的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

在我的应用程序中,如果用户可以生成音频流,则他可以拨打电话.所以,我需要访问麦克风(音频流).没有它,应用程序应该抛出一个错误.视频是可选的.所以,我调用 navigator.getUserMedia 并把 constraints 像这样:

In my application a user can make calls if he can produce an audio stream. So, I need to require access to the microphone (audio stream). Without it the application should throw an error. Video is optional. So, I call navigator.getUserMedia and put constraints like this:

{ 音频:真,视频:假 }

当没有麦克风时它会抛出错误,就像我需要的那样.但这样做的一个副作用是,如果用户还可以使用摄像头,则视频流中不会出现视频.

And it throws an error when a microphone is not present, just like I need. But a side effect from this is that if the user also has access to a camera, video is not present in stream.

但是如果我将 audiovideo 都设置为 true 我会在用户有麦克风但没有麦克风的情况下出错访问相机(根据我的应用程序逻辑可以)

But if I set both audio and video to true I would have an error in cases when users have a microphone but do not have access to a camera (which is ok according to my application logic)

如何让视频流成为可选的 get getUserMedia?

How I can make a video stream optional get getUserMedia?

推荐答案

我发现的一个解决方案是在启用视频和音频的情况下调用 getUserMedia,如果调用失败(意味着他们要么不没有摄像头或麦克风)然后您从您提供的仅请求访问麦克风的失败回调中再次调用 getUserMedia.

A solution I have found is to call getUserMedia with video and audio enabled, if the call fails(meaning that they either don't have a camera or a mic) then you call getUserMedia again from the failure callback that you provide requesting access to the microphone only.

var failedLocalAudioAndVideoStreamCallBack = function (error) {
      getUserMedia({ audio: true, video: false }, 
      gotLocalAudioStreamCallBack, failedLocalAudioStreamCallBack )});
    }

    getUserMedia({ audio: true, video: true },
    gotLocalAudioAndVideoStreamCallBack, failedLocalAudioAndVideoStreamCallBack); 

当然,您可以随心所欲地处理成功和失败.

Of course, you can handle successes and failures however you like.

注意:如果没有摄像头,则不会出现请求初始摄像头馈送(将失败)的弹出窗口.因此,用户只会收到一个访问请求(这使得该解决方案更可口).

NOTE: if there is no camera, the pop-up requesting the initial camera feed(that will fail) never occurs. So, the user will only get one request for access(which makes this solution a tad bit more palatable).

这篇关于在 getUserMedia 中将视频流设为可选的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-26 08:40:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1132393.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:设为   可选   中将   视频   getUserMedia

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!