Search code examples
angularjsgetusermedia

navigator.mediaDevices is Undefined only In an AngularJs Contraller


I'm trying to access javasctipt mediaDevices inside an AngularJs controller this is my controller code:

(function () {
    'use strict';

    angular.module('app')
        .controller('AssistenzaCtrl', ['$scope', 'largeTabController', 'readSelect', '$mdDialog', '$interval', '$http', '$window', AssistenzaCtrl])

    function AssistenzaCtrl($scope, largeTabController, readSelect, $mdDialog, $interval, $http, $window ) {

        $scope.init = function () {
            if (!navigator.mediaDevices || !navigator.mediaDevices.enumerateDevices) {
              console.log("enumerateDevices() not supported.");
            } 
            navigator.mediaDevices.enumerateDevices()
            .then(function(devices) {
              devices.forEach(function(device) {
                console.log(device.kind + ": " + device.label +
                            " id = " + device.deviceId);
              });
            })
            .catch(function(err) {
              console.log(err.name + ": " + err.message);
            });             
        }   
    }
})();   

If I test My controller with F12 tools navigator.mediaDevices is undefined but my browser supports mediaDevices because if I run the same code inside a standalone page without AngularJs I can successfully enumerate my devices.

Are the compatibility issues between AngularJs and getUserMedia() ?


Solution

  • The issue was related to the use of http protocol instead od https. The browser enables getUserMedia only for https or connections to localhost.

    I hope that this answer can help somebody.