{{APIRef("Media Capture and Streams")}}
The MediaDevices.ondevicechange
property is an {{domxref("EventHandler")}} which specifies a function to be called when the {{event("devicechange")}} event occurs on a {{domxref("MediaDevices")}} instance. This happens whenever the set of media devices available to the {{Glossary("user agent")}} and, by extension, to the web site or app. You can at any time use {{domxref("MediaDevices.enumerateDevices", "enumerateDevices()")}} to get the updated list of available devices.
Syntax
MediaDevices.ondevicechange = eventHandler;
Value
A function you provide which accepts as input a {{domxref("Event")}} object describing the {{domxref("devicehange")}} event that occurred. There is no information about the change included in the event object; to get the updated list of devices, you'll have to use {{domxref("MediaDevices.enumerateDevices", "enumerateDevices()")}}.
Example
This example adds a listener which, when a new track is added to the stream, appends a new item to a list of tracks; the new item shows the track's kind
("audio"
or "video"
) and label
.
stream.ondevicechange = function(event) { let trackList = document.getElementById("tracks"); let label = document.createElement("li"); label.innerHTML = event.track.kind + ": " + event.track.label; trackList.appendChild(label); };
Specifications
Specification | Status | Comment |
---|---|---|
{{ SpecName('Media Capture', '#event-mediastream-devicechange', 'devicechange') }} | {{ Spec2('Media Capture') }} | Initial specification. |
Browser compatibility
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|
Basic support | {{CompatVersionUnknown}} | {{CompatGeckoDesktop(51)}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} |
Feature | Android | Android Webview | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile | Chrome for Android |
---|---|---|---|---|---|---|---|
Basic support | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatNo}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatVersionUnknown}} |
[1] While support for the devicechange
event and for {{domxref("MediaDevices.ondevicechange")}} landed in Firefox 51, but only for Mac, and disabled by default. It can be enabled by setting the preference media.ondevicechange.enabled
to true. Support for this event was added for Linux and Windows—and it was enabled by default—starting in Firefox 52.
See also
- The {{event("devicechange")}} event and its type, {{domxref("Event")}}.
- {{domxref("MediaDevices.enumerateDevices()")}}
- {{domxref("MediaDeviceInfo")}}