博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
getusermedia_如何使用getUserMedia()
阅读量:2504 次
发布时间:2019-05-11

本文共 4597 字,大约阅读时间需要 15 分钟。

getusermedia

The MediaDevices object exposed by navigator.mediaDevices gives us the getUserMedia method.

MediaDevices对象通过暴露navigator.mediaDevices给我们的getUserMedia方法。

Warning: the navigator object exposes a getUserMedia() method as well, which might still work but is deprecated. The API has been moved inside the mediaDevices object for consistency purposes.

警告: navigator对象也公开了getUserMedia()方法,该方法可能仍然有效,但已弃用 。 为了保持一致性, mediaDevices API移到mediaDevices对象内。

This is how we can use this method.

这就是我们可以使用这种方法的方式。

Say we have a button:

假设我们有一个按钮:

We wait until the user clicks this button, and we call the navigator.mediaDevices.getUserMedia() method.

我们一直等到用户单击此按钮,然后调用navigator.mediaDevices.getUserMedia()方法。

We pass an object that describes the media constraints we want. If we want a video input we’ll call

我们传递一个描述所需媒体约束的对象。 如果我们要视频输入,我们会打电话给

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

We can be very specific with those constraints:

我们可以很具体地说明这些约束:

navigator.mediaDevices.getUserMedia({  video: {    minAspectRatio: 1.333,    minFrameRate: 60,    width: 640,    heigth: 480  }})

You can get a list of all the constraints supported by the device by calling navigator.mediaDevices.getSupportedConstraints().

您可以通过调用navigator.mediaDevices.getSupportedConstraints()获得设备支持的所有约束的列表。

If we just want the audio we can pass audio: true:

如果我们只想要音频,则可以传递audio: true

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

and if we want both:

如果我们都想要:

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

This method returns a promise, so we’ll use async/await to get the result in a stream variable:

此方法返回一个Promise,因此我们将使用async / await在stream变量中获取结果:

document.querySelector('button').addEventListener('click', async (e) => {  const stream = await navigator.mediaDevices.getUserMedia({    video: true  })})

Clicking the button will trigger a panel in the browser to allow for the permission to use the media devices:

单击该按钮将触发浏览器中的面板,以允许使用媒体设备的权限:

The permission screen

See on Codepen:

在Codepen上查看: ://codepen.io/flaviocopes/pen/WWyGmr

Once we are done, the stream object we got from getUserMedia() can now be used for various things. The most immediate is to display the video stream in a video element in the page:

一旦完成,从getUserMedia()获得的stream对象现在可以用于各种用途。 最直接的方法是在页面的video元素中显示视频流:

document.querySelector('button').addEventListener('click', async (e) => {  const stream = await navigator.mediaDevices.getUserMedia({    video: true  })  document.querySelector('video').srcObject = stream})

See on Codepen:

在Codepen上查看: ://codepen.io/flaviocopes/pen/wZXzbB

一个例子 (An example)

Here is a Codepen example that asks you to access the video camera and plays the video in the page:

这是一个Codepen示例,要求您访问摄像机并在页面中播放视频:

See the Pen by Flavio Copes () on .

见笔由弗拉维奥·科佩斯( 上) 。

We add a button to get access to the camera, then we add a video element, with the autoplay attribute.

我们添加一个按钮以访问摄像机,然后添加一个具有autoplay属性的video元素。

The JS listens for a click on the button, then calls navigator.mediaDevices.getUserMedia() asking for the video. Then we access the name of the camera used by calling stream.getVideoTracks() on the result of the call to getUserMedia().

JS监听按钮的单击,然后调用navigator.mediaDevices.getUserMedia()询问视频。 然后,我们访问摄像机的调用使用的名称stream.getVideoTracks()的调用的结果getUserMedia()

The stream is set to be the source object for the video tag, so that playback can happen:

流被设置为video标签的源对象,因此可以进行回放:

document.querySelector('#get-access').addEventListener('click', async function init(e) {  try {    const stream = await navigator.mediaDevices.getUserMedia({      video: true    })    document.querySelector('video').srcObject = stream    document.querySelector('#get-access').setAttribute('hidden', true)    setTimeout(() => { track.stop() }, 3 * 1000)  } catch (error) {    alert(`${error.name}`)    console.error(error)  }})

The arguments of getUserMedia() can specify additional requirements for the video stream:

getUserMedia()的参数可以指定视频流的其他要求:

navigator.mediaDevices.getUserMedia({  video: {    mandatory: { minAspectRatio: 1.333, maxAspectRatio: 1.334 },    optional: [      { minFrameRate: 60 },      { maxWidth: 640 },      { maxHeigth: 480 }    ]  }}, successCallback, errorCallback);

To get an audio stream you would ask for the audio media object too, and call stream.getAudioTracks() instead of stream.getVideoTracks().

要获取音频流,您还需要音频媒体对象,然后调用stream.getAudioTracks()而不是stream.getVideoTracks()

After 3 seconds of playback we stop the video streaming by calling track.stop().

播放3秒后,我们通过调用track.stop()停止视频流。

翻译自:

getusermedia

转载地址:http://fmqgb.baihongyu.com/

你可能感兴趣的文章
Entity Framework 4.3.1 级联删除
查看>>
codevs 1163:访问艺术馆
查看>>
冲刺Noip2017模拟赛3 解题报告——五十岚芒果酱
查看>>
并查集
查看>>
sessionStorage
查看>>
代码示例_进程
查看>>
Java中关键词之this,super的使用
查看>>
人工智能暑期课程实践项目——智能家居控制(一)
查看>>
前端数据可视化插件(二)图谱
查看>>
kafka web端管理工具 kafka-manager【转发】
查看>>
获取控制台窗口句柄GetConsoleWindow
查看>>
Linux下Qt+CUDA调试并运行
查看>>
51nod 1197 字符串的数量 V2(矩阵快速幂+数论?)
查看>>
OKMX6Q在ltib生成的rootfs基础上制作带QT库的根文件系统
查看>>
zabbix
查看>>
多线程基础
查看>>
完美解决 error C2220: warning treated as error - no ‘object’ file generated
查看>>
使用SQL*PLUS,构建完美excel或html输出
查看>>
SQL Server数据库笔记
查看>>
X-Forwarded-For伪造及防御
查看>>