5-1 【基础铺垫,学前有概念】WebRTC获取音视频设备

图片.png
图片.png

5-2 【实战】在页面中显示获取到的设备

public下面新建目录device,新建文件
图片.png

代码
index.html

  1. <html>
  2. <head>
  3. <title> WebRTC get audio and video devices</title>
  4. </head>
  5. <body>
  6. <div>
  7. <label>audio input device:</label>
  8. <select id="audioSource"></select>
  9. </div>
  10. <div>
  11. <label>audio output device:</label>
  12. <select id="audioOutput"></select>
  13. </div>
  14. <div>
  15. <label>video input device:</label>
  16. <select id="videoSource"></select>
  17. </div>
  18. <script src="./js/client.js"></script>
  19. </body>
  20. </html>

client.js

  1. 'use strict'
  2. var audioSource = document.querySelector("select#audioSource");
  3. var audioOutput = document.querySelector("select#audioOutput");
  4. var videoSource = document.querySelector("select#videoSource");
  5. if(!navigator.mediaDevices ||
  6. !navigator.mediaDevices.enumerateDevices){
  7. console.log('enumerateDevices is not supported!');
  8. }else {
  9. navigator.mediaDevices.enumerateDevices()
  10. .then(gotDevices)
  11. .catch(handleError);
  12. }
  13. function gotDevices(deviceInfos){
  14. deviceInfos.forEach( function(deviceInfo){
  15. console.log(deviceInfo.kind + ": label = "
  16. + deviceInfo.label + ": id = "
  17. + deviceInfo.deviceId + ": groupId = "
  18. + deviceInfo.groupId);
  19. var option = document.createElement('option');
  20. option.text = deviceInfo.label;
  21. option.value = deviceInfo.deviceId;
  22. if(deviceInfo.kind === 'audioinput'){
  23. audioSource.appendChild(option);
  24. }else if(deviceInfo.kind === 'audiooutput'){
  25. audioOutput.appendChild(option);
  26. }else if(deviceInfo.kind === 'videoinput'){
  27. videoSource.appendChild(option);
  28. }
  29. });
  30. }
  31. function handleError(err){
  32. console.log(err.name + " : " + err.message);
  33. }

http查看
图片.png

需要使用https才能查看
图片.png
图片.png