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
Back
Top Bottom