<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/accessing-your-webcam-in-html5-with-getusermedia-lsf7yrwbc" -->

---
title: Accessing Your Webcam in HTML5 with getUserMedia | daily.dev
description: You can show your webcam's live video in the browser with plain HTML and JavaScript. Add a video element with autoplay and playsinline, then call...
canonical: https://daily.dev/posts/accessing-your-webcam-in-html5-with-getusermedia-lsf7yrwbc
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Accessing Your Webcam in HTML5 with getUserMedia | daily.dev
og:description: You can show your webcam's live video in the browser with plain HTML and JavaScript. Add a video element with autoplay and playsinline, then call...
og:url: https://daily.dev/posts/accessing-your-webcam-in-html5-with-getusermedia-lsf7yrwbc
og:image: https://api.daily.dev/og/posts/lsF7Yrwbc.png
og:image:alt: Accessing Your Webcam in HTML5 with getUserMedia
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Accessing Your Webcam in HTML5 with getUserMedia

**[Kirupa](https://daily.dev/sources/kirupa)** · 16 min read · 1 upvotes · 0 comments

## Summary

You can show your webcam's live video in the browser with plain HTML and JavaScript. Add a video element with autoplay and playsinline, then call navigator.mediaDevices.getUserMedia({ video: true }) and assign the returned stream to the element's srcObject. Pages must be served over HTTPS, though localhost works for testing. The error name in the catch block (NotAllowedError, NotFoundError, NotReadableError) tells you why access failed. Set facingMode: 'environment' to pick the back camera. Call stop() on each track from getTracks() to turn the camera off, since hiding the video element leaves it running.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.kirupa.com/html5/accessing_your_webcam_in_html5.htm>

## Questions this post answers

### Why is navigator.mediaDevices undefined when I try to use my webcam with getUserMedia?

Browsers only expose webcam access on pages served over HTTPS (a secure context), and on a plain http:// page navigator.mediaDevices does not exist. Local testing still works on http://localhost and http://127.0.0.1. Optional chaining (navigator.mediaDevices?.getUserMedia) avoids a thrown error. Webcam code inside a cross-site iframe also needs allow="camera".

_Developers debugging browser media APIs keep up with web platform changes on daily.dev._

### How do I use the back camera on a phone with getUserMedia?

Pass a constraints object with a video property containing facingMode: "environment", such as getUserMedia({ video: { facingMode: "environment" } }). A value of "user" requests the selfie camera. You can add width and height with ideal values; using exact instead can fail with an OverconstrainedError if the camera cannot deliver.

_Building camera features for mobile web is easier when you track what browsers now support, on daily.dev._

### How do I properly stop a webcam stream in JavaScript so the camera light turns off?

Get the stream from the video element's srcObject, call getTracks() on it, and call stop() on every track. That releases the webcam and turns off its light. Then set srcObject to null to clear the video element. Hiding or removing the video element is not enough, because the camera keeps running while tracks are active.

_Developers handling browser media streams follow practical web API tips on daily.dev._

## Similar posts on daily.dev

- [Designing Ephemeral Video Chat Sessions with WebRTC and JavaScript](https://daily.dev/posts/designing-ephemeral-video-chat-sessions-with-webrtc-and-javascript-9avi8wcbx) · SitePoint · 1 upvotes · 1 comments
- [Record and share the screen from the browser — getDisplayMedia, and why Electron does the picker differently](https://daily.dev/posts/record-and-share-the-screen-from-the-browser-getdisplaymedia-and-why-electron-does-the-picker-dif-7ozwcimtz) · Medium · 0 upvotes · 0 comments
- [Help us shape device audio capture on the web](https://daily.dev/posts/help-us-shape-device-audio-capture-on-the-web-2dclag3fs) · Patrick Brosset · 0 upvotes · 0 comments
- [How to do video processing in the browser with WebCodecs](https://daily.dev/posts/how-to-do-video-processing-in-the-browser-with-webcodecs-mvxqpopnb) · SitePoint · 0 upvotes · 0 comments

---

Tags: [#webdev](https://daily.dev/tags/webdev), [#javascript](https://daily.dev/tags/javascript), [#html](https://daily.dev/tags/html), [#webrtc](https://daily.dev/tags/webrtc)

[View this post on daily.dev](https://daily.dev/posts/accessing-your-webcam-in-html5-with-getusermedia-lsf7yrwbc)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Accessing Your Webcam in HTML5 with getUserMedia","url":"https://daily.dev/posts/accessing-your-webcam-in-html5-with-getusermedia-lsf7yrwbc","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/accessing-your-webcam-in-html5-with-getusermedia-lsf7yrwbc"},"datePublished":"2026-11-28T06:30:37.695Z","dateModified":"2026-11-28T06:31:03.609Z","description":"You can show your webcam's live video in the browser with plain HTML and JavaScript. Add a video element with autoplay and playsinline, then call...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/85f6240f5a4a33fa9f31679dec342c55?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/85f6240f5a4a33fa9f31679dec342c55?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Kirupa","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Kirupa","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/ff2fdb4927d34b81bd07cd18d6049a98","url":"https://daily.dev/sources/kirupa"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/accessing-your-webcam-in-html5-with-getusermedia-lsf7yrwbc","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":1},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"webdev,javascript,html,webrtc","timeRequired":"PT16M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Kirupa","item":"https://daily.dev/sources/kirupa"},{"@type":"ListItem","position":3,"name":"Accessing Your Webcam in HTML5 with getUserMedia"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/accessing-your-webcam-in-html5-with-getusermedia-lsf7yrwbc#faq","mainEntity":[{"@type":"Question","name":"Why is navigator.mediaDevices undefined when I try to use my webcam with getUserMedia?","acceptedAnswer":{"@type":"Answer","text":"Browsers only expose webcam access on pages served over HTTPS (a secure context), and on a plain http:// page navigator.mediaDevices does not exist. Local testing still works on http://localhost and http://127.0.0.1. Optional chaining (navigator.mediaDevices?.getUserMedia) avoids a thrown error. Webcam code inside a cross-site iframe also needs allow=\"camera\". Developers debugging browser media APIs keep up with web platform changes on daily.dev."}},{"@type":"Question","name":"How do I use the back camera on a phone with getUserMedia?","acceptedAnswer":{"@type":"Answer","text":"Pass a constraints object with a video property containing facingMode: \"environment\", such as getUserMedia({ video: { facingMode: \"environment\" } }). A value of \"user\" requests the selfie camera. You can add width and height with ideal values; using exact instead can fail with an OverconstrainedError if the camera cannot deliver. Building camera features for mobile web is easier when you track what browsers now support, on daily.dev."}},{"@type":"Question","name":"How do I properly stop a webcam stream in JavaScript so the camera light turns off?","acceptedAnswer":{"@type":"Answer","text":"Get the stream from the video element's srcObject, call getTracks() on it, and call stop() on every track. That releases the webcam and turns off its light. Then set srcObject to null to clear the video element. Hiding or removing the video element is not enough, because the camera keeps running while tracks are active. Developers handling browser media streams follow practical web API tips on daily.dev."}}]}
```

