Webcam in HTML


I found two problems with this html.

First the already mentioned problem of https. The official documentation of the getUserMedia function mentions this limitation, the connection must be secure or it would fail. You could try from localhost or a plain file (which is a better idea for developing anyway) and test if it works there.
See the relevant documentation here: MediaDevices: getUserMedia() method - Web APIs | MDN

Then, I've loaded your sample file into a real browser and found a Javascript error (look in the console for it):

Uncaught TypeError: document.queryselector is not a function
at example.html:37:24
Uncaught ReferenceError: startCam is not defined
at HTMLButtonElement.onclick (example.html:54:82)

The first one is thrown right after opening the file, the second one upon clicking the button, which explains why the cam doesn't even attempts to load.

After a bit of debugging. The first problem is that, on line 37, there is a case error, it's "queryselector" and it should be "querySelector" (note the capital S).
After that, the browser requested webcam permission, but the console said "something went wrong". Tip here, always add the error description in such logs to see why something went wrong. I modified line 45 to read:
console.log("something went wrong", error);
Then the console show this:

something went wrong TypeError: Cannot set properties of null (setting 'srcObject')
at example.html:42:21

This hints that the video variable is null. As it's initialized again in line 37, it means that the selector couldn't find the video element. If you look there, the call to querySelector is "videoelement", but since you want a perticular element it must read "#videoelement" to identify an element by its id.
with this change the webcam started working right away.

I haven't digged into the start / stop buttons, but they also threw errors.
I left the changed file attached here for reference.
Thanks for looking into that.
I had my own working version written which prompts to allow the use of the device and then the cam works from there.
So, I didn't need to use the OP's code.
As you mention, the real showstopper cannot be bypassed and that is the requirement he was looking to overcome.
 

My Computers My Computers

  • At a glance

    All Branches but StableAMD Ryzen 7 7735HS 3200-4500 Mhz 8 cores x 232 GB DDR5Radeon Graphic / NVIDIA GeForce RTX 4060 8 GB...
    OS
    All Branches but Stable
    Computer type
    Laptop
    Manufacturer/Model
    Acer Nitro ANV15-51
    CPU
    AMD Ryzen 7 7735HS 3200-4500 Mhz 8 cores x 2
    Motherboard
    Sportage_RBH
    Memory
    32 GB DDR5
    Graphics Card(s)
    Radeon Graphic / NVIDIA GeForce RTX 4060 8 GB GDDR6
    Sound Card
    AMD/Realtek(R) Audio
    Monitor(s) Displays
    Integrated Monitor (15.3"vis)
    Screen Resolution
    FHD 1920X1080 16:9 144Hz
    Hard Drives
    KINGSTON OM8SEP4512Q-AA 1TB
    Western Digital 256GB
    PSU
    19V DC 6.32 A 120 W
    Cooling
    Dual Fans
    Mouse
    MS Bluetooth
    Internet Speed
    Fiber 1GB Cox -us & 1GB Orange-fr
    Browser
    Edge Canary- Firefox Nightly-Chrome Dev-Chrome Dev
    Antivirus
    Windows Defender
  • At a glance

    Windows 11 BetaAMD A9-94208 GB of DDR4AMD Radeon R5
    Operating System
    Windows 11 Beta
    Computer type
    Laptop
    Manufacturer/Model
    Asus X751BP
    CPU
    AMD A9-9420
    Memory
    8 GB of DDR4
    Graphics card(s)
    AMD Radeon R5
    Screen Resolution
    1600x900
    Hard Drives
    Seagate 1 TB
Alejandro85, thank you so much! I'll read through this tomorrow when I'm spending time on my website. And yes I was looking to overcome the https, I'll deal with that too.
 

My Computer My Computer

At a glance

Windows 11
OS
Windows 11
Computer type
Laptop
Manufacturer/Model
HP
Hi Alejandro85, So I read everything you said. I did add https to my website :), your welcome everyone. I also made the changes you mentioned. So here is where I am. From my host machine, I can load the cam using localhost over https, as well as the ip address. When I try to load the cam from another machine, on the same network, over https after adding a Windows firewall rule(on the host machine), the web page loads. However, I get this prompt that I have never seen (Starting Droid Cam). ??
 

My Computer My Computer

At a glance

Windows 11
OS
Windows 11
Computer type
Laptop
Manufacturer/Model
HP
Hi All,

Well somewhere in this thread or another on this forum someone mentioned that when the usb cam is loaded in the website, it would be pulling the local camera of the client machine that is loaded the website from the server. So in end with all your help I got the camera function to load within my website. I guess I was hoping to somehow pull the camera feed from the server to the host. I'll have to look into this. Maybe this requires more than just html/java code. Thanks all..
 

My Computer My Computer

At a glance

Windows 11
OS
Windows 11
Computer type
Laptop
Manufacturer/Model
HP
HTML and javascript runs entirely in the browser, in the computer you open the website from, that's why you can, at most, use cameras from your computer and that's what you see.

To watch cameras installed on the server you need something completely outside the realm of html/javascript/css. You need something running at the server, picking the camera there, then exposing that feed though http for the browser to display. There are quite a few ways you can do this, but your sample file is not geared there.
 

My Computer My Computer

At a glance

Windows 11
OS
Windows 11
Computer type
PC/Desktop
HTML and javascript runs entirely in the browser, in the computer you open the website from, that's why you can, at most, use cameras from your computer and that's what you see.

To watch cameras installed on the server you need something completely outside the realm of html/javascript/css. You need something running at the server, picking the camera there, then exposing that feed though http for the browser to display. There are quite a few ways you can do this, but your sample file is not geared there.
If I remember correctly you can stream your webcam with Jellyfin and of course with OSB Studio.
 

My Computers My Computers

  • At a glance

    Windows 11 Pro 23H2 (7584), 26H2 (9711)Mobile DualCore Intel Core 2 Duo T7250, 2000 MHz4GBMobile Intel(R) GMA 4500M (Mobile 4 series)
    OS
    Windows 11 Pro 23H2 (7584), 26H2 (9711)
    Computer type
    Laptop
    Manufacturer/Model
    Acer Extensa 5630EZ
    CPU
    Mobile DualCore Intel Core 2 Duo T7250, 2000 MHz
    Motherboard
    Acer Extensa 5630
    Memory
    4GB
    Graphics Card(s)
    Mobile Intel(R) GMA 4500M (Mobile 4 series)
    Sound Card
    Realtek ALC268 @ Intel 82801IB ICH9 - High Definition Audio Controller
    Monitor(s) Displays
    1
    Screen Resolution
    1280x800
    Hard Drives
    Samsung SSD 850 EVO 250GB SATA Device (250 GB, SATA-III)
    Internet Speed
    802.11g wireless 54 Mbps
    Browser
    MICROSOFT EDGE
    Antivirus
    WINDOWS DEFENDER
    Other Info
    Legacy MBR installation, no TPM, no Secure Boot, no WDDM 2.0 graphics drivers, no SSE4.2, cannot get more unsupported ;) This is only my test laptop. I had installed Windows 11 here before upgrading my main PC. For my main PC I use everyday see my 2nd system specs.
  • At a glance

    Windows 11 Pro 26H2 (build 26300.9711)Intel Core-i7 3770 3.40GHz s1155 (3rd generat...2x Kingston Hyper-X Blu 8GB DDR3-1600GIGABYTE GeForce RTX 3050 WINDFORCE OC V2 6GB...
    Operating System
    Windows 11 Pro 26H2 (build 26300.9711)
    Computer type
    PC/Desktop
    Manufacturer/Model
    Custom-built PC
    CPU
    Intel Core-i7 3770 3.40GHz s1155 (3rd generation)
    Motherboard
    Asus P8H61 s1155 ATX
    Memory
    2x Kingston Hyper-X Blu 8GB DDR3-1600
    Graphics card(s)
    GIGABYTE GeForce RTX 3050 WINDFORCE OC V2 6GB (GV-N3050WF2OCV2-6GD)
    Sound Card
    Realtek HD audio (ALC887)
    Monitor(s) Displays
    Sony Bravia KDL-19L4000 19" LCD TV via VGA
    Screen Resolution
    1440x900 32-bit 60Hz
    Hard Drives
    WD Blue SA510 2.5 1000GB SSD as system disk, Western Digital Caviar Purple 4TB SATA III (WD40PURZ) as second
    PSU
    Thermaltake Litepower RGB 550W Full Wired
    Case
    SUPERCASE MIDI-TOWER
    Cooling
    Deepcool Gamma Archer CPU cooler, 1x 8cm fan at the back
    Keyboard
    Mitsumi 101-key PS/2
    Mouse
    Microsoft Compact Optical Mouse
    Internet Speed
    802.11ac 5GHz wireless 260Mpbs
    Browser
    Microsoft Edge, Mozilla Firefox
    Antivirus
    Microsoft Windows Defender
    Other Info
    Legacy BIOS (MBR) installation, no TPM, no Secure Boot, WDDM 3.0 graphics drivers, WEI score 7.4

Latest Support Threads

Back
Top Bottom