如何使用JavaScript的getUserMedia()访问前后摄像头

本文介绍了如何使用HTML5的MediaDevices API访问设备的前后摄像头。通过getUserMedia方法,开发者可以请求用户权限,设置媒体约束并在浏览器上显示视频流。教程详细讲解了检查设备支持、请求权限、理解媒体约束以及实际操作的步骤,适合具备JavaScript基础知识的读者学习。
摘要由CSDN通过智能技术生成

介绍 (Introduction)

With HTML5 came the introduction of APIs with access to device hardware, including the MediaDevices API. This API provides access to media input devices like audio and video.

随着HTML5的出现,引入了可以访问设备硬件的API,包括MediaDevices API。 该API提供对媒体输入设备(如音频和视频)的访问。

With this API’s help, developers can access audio and video devices to stream and display live video feeds in the browser. In this tutorial, you’ll access the video feed from the user’s device and display it in the browser using the getUserMedia method.

借助该API的帮助,开发人员可以访问音频和视频设备,以在浏览器中流式传输和显示实时视频。 在本教程中,您将从用户设备访问视频源,并使用getUserMedia方法在浏览器中显示它。

The getUserMedia API makes use of the media input devices to produce a MediaStream. This MediaStream contains the requested media types, whether audio or video. Using the stream returned from the API, video feeds can be displayed on the browser, which is useful for real-time communication on the browser.

getUserMedia API使用媒体输入设备来产生MediaStream 。 此MediaStream包含请求的媒体类型,无论是音频还是视频。 使用从API返回的流,可以在浏览器上显示视频供稿,这对于在浏览器上进行实时通信很有用。

When used along with the MediaStream Recording API, you can record and store media data captured on the browser. This API only works on secure origins like the rest of the newly introduced APIs, but it also works on localhost and file URLs.

MediaStream Recording API一起使用时,您可以记录和存储在浏览器中捕获的媒体数据。 该API仅适用于像其他新引入的API一样的安全来源,但也适用于localhost和文件URL。

先决条件 (Prerequisites)

This tutorial will first explain concepts and demonstrate examples with Codepen. In the final step, you will create a functioning video feed for the browser.

本教程将首先解释概念并使用Codepen演示示例。 在最后一步,您将为浏览器创建一个正常运行的视频提要。

步骤1 —检查设备支持 (Step 1 — Checking Device Support)

First, you will see how to check if the user’s browser supports the mediaDevices API. This API exists within the navigator interface and contains the current state and identity of the user agent. The check is performed with the following code that can be pasted into Codepen:

首先,您将看到如何检查用户的浏览器是否支持mediaDevices API。 该API存在于导航器界面中,并包含用户代理的当前状态和身份。 使用以下可粘贴到Codepen中的代码执行检查:

if ('mediaDevices' in navigator && 'getUserMedia' in navigator.mediaDevices) {
  console.log("Let's get this party started")
}

First, this checks if the mediaDevices API exists within the navigator and then checks if the getUserMedia API is available within the mediaDevices. If this returns true, you can get started.

首先,这将检查navigator是否存在mediaDevices API,然后检查mediaDevicesgetUserMedia API是否可用。 如果返回true ,则可以开始。

第2步-请求用户权限 (Step 2 — Requesting User Permission)

After confirming the browser’s support for getUserMedia, you need to request permission to make use of the media input devices on the user agent. Typically, after a user grants permission, a Promise is returned which resolves to a media stream. This Promise isn’t returned when the permission is denied by the user, which blocks access to these devices.

确认浏览器对getUserMedia的支持后,您需要请求权限才能使用用户代理上的媒体输入设备。 通常,在用户授予权限后,将返回一个Promise ,该Promise会解析为媒体流。 当用户拒绝此权限(阻止访问这些设备)时,不会返回此Promise

Paste the following line into Codepen to request permission:

将以下行粘贴到Codepen中以请求权限:

navigator.mediaDevices.getUserMedia({video: true})

The ob

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值