--- url: "https://web.dev/articles/getusermedia-intro" titulek: "Capture audio and video in HTML5 | Articles | web.dev" h1: "Capture audio and video in HTML5 Stay organized with collections Save and categorize content based on your preferences." popis: "" jazyk: "en" kanonicka: "https://web.dev/articles/getusermedia-intro" drobecky: ["Articles", "Capture audio and video in HTML5"] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-14 00:57:03" znaku: 10395 slov: 1412 kodovani: "iso-8859-2" sha256: "f9dd629048b85326a4db7da28c54dd3315371c90849f37813404623f713667e7" --- Capture audio and video in HTML5 Stay organized with collections Save and categorize content based on your preferences. Eric Bidelman Sam Dutton Introduction Audio/Video capture has been *the* "Holy Grail" of web development for a long time. For many years we've had to rely on browser plugins ([Flash](http://www.kevinmusselman.com/2009/02/access-webcam-with-flash/) or [Silverlight](http://www.silverlightshow.net/items/Capturing-the-Webcam-in-Silverlight-4.aspx)) to get the job done. [Come on!](https://www.youtube.com/watch?v=SP_9zH9Q44o) HTML5 to the rescue. It might not be apparent, but the rise of HTML5 has brought a surge of access to device hardware. [Geolocation](https://caniuse.com/?search=geolocation) (GPS), the [Orientation API](https://caniuse.com/screen-orientation) (accelerometer), [WebGL](https://caniuse.com/?search=webgl) (GPU), and the [Web Audio API](https://caniuse.com/?search=webgl) (audio hardware) are perfect examples. These features are ridiculously powerful, exposing high level JavaScript APIs that sit on top of the system's underlying hardware capabilities. This tutorial introduces a new API, [GetUserMedia](https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia), which allows web apps to access a user's camera and microphone. The road to getUserMedia() If you're not aware of its history, the way we arrived at the `getUserMedia()` API is an interesting tale. Several variants of "Media Capture APIs" have evolved over the past few years. Many folks recognized the need to be able to access native devices on the web, but that led everyone and their mom to put together a new spec. Things got so messy that the W3C finally decided to form a working group. Their sole purpose? Make sense of the madness! The [Device APIs Policy (DAP) Working Group](http://www.w3.org/2009/dap/) has been tasked to consolidate + standardize the plethora of proposals. I'll try to summarize what happened in 2011… Round 1: HTML Media Capture [HTML Media Capture](http://dev.w3.org/2009/dap/camera/) was the DAP's first go at standardizing media capture on the web. It works by overloading the `` and adding new values for the `accept` parameter. If you wanted to let users take a snapshot of themselves with the webcam, that's possible with `capture=camera`: `` Recording a video or audio is similar: ` ` Kinda nice right? I particularly like that it reuses a file input. Semantically, it makes a lot of sense. Where this particular "API" falls short is the ability to do realtime effects (e.g. render live webcam data to a `` and apply WebGL filters). HTML Media Capture only allows you to record a media file or take a snapshot in time. **Support:** [Android 3.0 browser](http://developer.android.com/sdk/android-3.0.html) - one of the first implementations. Check out [this video](http://davidbcalhoun.com/2011/android-3-0-honeycomb-is-first-to-implement-the-device-api) to see it in action. Chrome for Android (0.16) Firefox Mobile 10.0 iOS6 Safari and Chrome (partial support) Round 2: device element Many thought HTML Media Capture was too limiting, so a new spec emerged that supported any type of (future) device. Not surprisingly, the design called for a new element, the [`` element](http://dev.w3.org/html5/html-device/), which became the predecessor to `getUserMedia()`. Opera was among the first browsers to create [initial implementations](http://my.opera.com/core/blog/2011/03/14/web-meet-device) of video capture based on the `` element. Soon after ([the same day](http://my.opera.com/core/blog/2011/03/23/webcam-orientation-preview) to be precise), the WhatWG decided to scrap the `` tag in favor of another up and comer, this time a JavaScript API called `navigator.getUserMedia()`. A week later, Opera put out new builds that included support for the updated `getUserMedia()` spec. Later that year, Microsoft joined the party by releasing a [Lab for IE9](http://blogs.msdn.com/b/ie/archive/2011/12/09/media-capture-api-helping-web-developers-directly-import-image-video-and-sound-data-into-web-apps.aspx) supporting the new spec. Here's what `` would have looked like: ` ` **Support:** Unfortunately, no released browser ever included ``. One less API to worry about I guess :) `` did have two great things going for it though: 1.) it was semantic, and 2.) it was easily extendible to support more than just audio/video devices. Take a breath. This stuff moves fast! Round 3: WebRTC The `` element eventually went the way of the Dodo. The pace to find a suitable capture API accelerated thanks to the larger [WebRTC](https://www.w3.org/TR/webrtc/) (Web Real Time Communications) effort. That spec is overseen by the [W3C WebRTC Working Group](http://www.w3.org/2011/04/webrtc/). Google, Opera, Mozilla, and [a few others](http://webrtc.org) have implementations. `getUserMedia()` is related to WebRTC because it's the gateway into that set of APIs. It provides the means to access the user's local camera/microphone stream. **Support:** `getUserMedia()` has been supported since Chrome 21, Opera 18, and Firefox 17. Getting started With `navigator.mediaDevices.getUserMedia()`, we can finally tap into webcam and microphone input without a plugin. Camera access is now a call away, not an install away. It's baked directly into the browser. Excited yet? Feature detection Feature detecting is a simple check for the existence of `navigator.mediaDevices.getUserMedia`: `if (navigator.mediaDevices?.getUserMedia) { // Good to go! } else { alert("navigator.mediaDevices.getUserMedia() is not supported"); }` Gaining access to an input device To use the webcam or microphone, we need to request permission. The first parameter to `navigator.mediaDevices.getUserMedia()` is an object specifying the details and requirements for each type of media you want to access. For example, if you want to access the webcam, the first parameter should be `{video: true}`. To use both the microphone and camera, pass `{video: true, audio: true}`: ` ` OK. So what's going on here? Media capture is a perfect example of new HTML5 APIs working together. It works in conjunction with our other HTML5 buddies, `