Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
navigator.mozGetUserMediaDevices Example
<html>
<body>
<h1>Device Test</h1>
<ul id="contents">
</ul>
<video id="tehvideo"/>
<audio id="tehaudio"/>
<script>
var devices;
var nav = navigator.QueryInterface(Components.interfaces.nsINavigatorUserMedia);
nav.mozGetUserMediaDevices(function(d) {
devices = d;
for (var i = 0; i < d.length; i++) {
var device = d[i].QueryInterface(Components.interfaces.nsIMediaDevice);
var t = document.createElement("li");
var inner = device.type + ": ";
inner += "<a href='#' onclick='startIt(" + i + ");'>" + device.name + "</a>";
t.innerHTML = inner;
document.getElementById("contents").appendChild(t);
};
}, function(e) { alert("Error! " + e); });
function startIt(num) {
var output, params;
var d = devices[num].QueryInterface(Components.interfaces.nsIMediaDevice);
if (d.type == "video") {
params = {video:true, device:d};
output = document.getElementById("tehvideo");
} else {
params = {audio:true, device:d};
output = document.getElementById("tehaudio");
}
navigator.mozGetUserMedia(params, function(s) {
output.src = s;
output.play();
}, function(e) {
alert("Error! " + e);
});
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment