0

I have been using getusermedia for pulling multiple cameras, I'm calling it using a separate .js file, it works fine, but I need the selector tag to show all its options available in the array when the page loads. I tried onload, but not working, please let me know if there is anyway out.

<div id="container">

    <label for="videoSource">Video source: </label><select id="videoSource"></select>

</div>

external js file

var videoSelect = document.querySelector('select#videoSource');
var selectors = [videoSelect];

function gotDevices(deviceInfos) {
  // Handles being called several times to update labels. Preserve values.
  var values = selectors.map(function(select) {
    return select.value;

  });
  selectors.forEach(function(select) {
    while (select.firstChild) {
      select.removeChild(select.firstChild);
    }
  });
  for (var i = 0; i !== deviceInfos.length; ++i) {
    var deviceInfo = deviceInfos[i];
    var option = document.createElement('option');
    option.value = deviceInfo.deviceId;
   if (deviceInfo.kind === 'videoinput') {
      option.text = deviceInfo.label || 'camera ' + (videoSelect.length + 1);
      videoSelect.appendChild(option);

    } else {
      console.log('Some other kind of source/device: ', deviceInfo);
    }
  }
  selectors.forEach(function(select, selectorIndex) {
    if (Array.prototype.slice.call(select.childNodes).some(function(n) {
      return n.value === values[selectorIndex];
    })) {
      select.value = values[selectorIndex];
    }
  });
}

navigator.mediaDevices.enumerateDevices().then(gotDevices).catch(handleError);

// Attach audio output device to video element using device/sink ID.
function attachSinkId(element, sinkId) {
  if (typeof element.sinkId !== 'undefined') {
    element.setSinkId(sinkId)
    .then(function() {
      console.log('Success, audio output device attached: ' + sinkId);
    })
    .catch(function(error) {
      var errorMessage = error;
      if (error.name === 'SecurityError') {
        errorMessage = 'You need to use HTTPS for selecting audio output ' +
            'device: ' + error;
      }
      console.error(errorMessage);
      // Jump back to first output device in the list as it's the default.
   //   audioOutputSelect.selectedIndex = 0;
    });
  } else {
    console.warn('Browser does not support output device selection.');
  }
}


function gotStream(stream) {
  window.stream = stream; // make stream available to console
  videoElement.srcObject = stream;
  // Refresh button list in case labels have become available
  return navigator.mediaDevices.enumerateDevices();
}

function start() {
  if (window.stream) {
    window.stream.getTracks().forEach(function(track) {
      track.stop();
    });
  }

  var videoSource = videoSelect.value;
  var constraints = {
    video: {deviceId: videoSource ? {exact: videoSource} : undefined}
  };
  navigator.mediaDevices.getUserMedia(constraints).
      then(gotStream).then(gotDevices).catch(handleError);
}


videoSelect.onchange = start;

start();

The method which I tried, not working..

function show(){
    document.getElementById("videoSource");

 }
 $(document).ready(function(){   
   show();
});
xShirase
  • 11,975
  • 4
  • 53
  • 85
andy ram
  • 213
  • 3
  • 21
  • function show(){ document.getElementById("videoSource").click() } – Dmitri Tsoy Nov 28 '16 at 13:52
  • As per @DmitriTsoy comment - you missed off the .click(). – Vanquished Wombat Nov 28 '16 at 13:59
  • @DmitriTsoy thanks for the fast reply, i tried it but no luck, i gave a alert to check, the alert seems to display but not the click(); function show(){ document.getElementById("videoSource").click(); alert("nope"); } $(document).ready(function(){ show(); }); – andy ram Nov 28 '16 at 14:17
  • Possible duplicate of [How can you programmatically tell an HTML SELECT to drop down (for example, due to mouseover)?](http://stackoverflow.com/questions/249192/how-can-you-programmatically-tell-an-html-select-to-drop-down-for-example-due) – YemSalat Nov 28 '16 at 14:44

0 Answers0