最新浏览器中getUserMedia抛出DevicesNotFoundError问题求助
Hey there, let's work through this getUserMedia problem you're hitting after browser updates. Those versions introduced some key changes that are likely breaking your existing code—here are the most common fixes:
1. Replace the deprecated navigator.getUserMedia with the modern API
The old callback-based navigator.getUserMedia has been phased out, and browsers like Chrome 63+ and Firefox 57+ are restricting support for it. The modern standard is navigator.mediaDevices.getUserMedia, which uses Promises for cleaner, more consistent error handling.
Here's how to update your code:
// Modern replacement for your original code navigator.mediaDevices.getUserMedia({ "audio": true, "video": false }) .then(function(stream) { console.log(stream); localStream = stream; }) .catch(function(err) { console.error("Error accessing media devices:", err); });
2. Ensure you're using HTTPS (except for local development)
Chrome 60+ enforces a secure context requirement for media device access. That means unless you're testing on localhost, 127.0.0.1, or a private IP, your site must be served over HTTPS. If you're running your code on a public domain without HTTPS, the browser will block getUserMedia requests entirely.
For local development, localhost still works fine—no need for HTTPS there. For production, you can set up a free SSL certificate via services like Let's Encrypt.
3. Double-check browser permissions
Browser updates sometimes reset site permissions. Make sure your browser hasn't revoked access to your microphone:
- In Chrome: Click the lock icon in the address bar → Site settings → Ensure "Microphone" is set to "Allow".
- In Firefox: Click the info icon (i) in the address bar → Permissions → Set "Microphone" to "Allow".
If permissions were set to "Block", that would explain why getUserMedia fails silently or throws a permission error.
4. Check for browser policy restrictions
If you're working in a corporate environment, there might be group policies or enterprise settings that block media device access. Reach out to your IT team to confirm if there are any restrictions on Chrome or Firefox that would prevent access to audio devices.
内容的提问来源于stack exchange,提问作者Akshay komarla

