{"id":13415,"date":"2022-02-09T18:41:58","date_gmt":"2022-02-09T18:41:58","guid":{"rendered":"http:\/\/www.max-sperling.bplaced.net\/?p=13415"},"modified":"2024-02-16T10:47:45","modified_gmt":"2024-02-16T10:47:45","slug":"mediadevices-audio-example","status":"publish","type":"post","link":"http:\/\/www.max-sperling.bplaced.net\/?p=13415","title":{"rendered":"MediaDevices (Audio example)"},"content":{"rendered":"<p>This example records from the selected audio input device (microphone) and plays to the selected audio output device (speaker). Be aware of a possible audio loop.<\/p>\n<p>The web browser will block you from getting a proper media device list when just running on filesystem files. You&#8217;ll have to host them on a web server, which can be local.<\/p>\n<p>For testing purposes under Windows just activate IIS under the system features. The localhost files are kept under &#8216;C:\\inetpub\\wwwroot&#8217;. Put the following files in a subfolder. Then open them with the web browser at &#8216;localhost\/&lt;subfolder&gt;&#8217;.<\/p>\n<hr>\n<p><strong>index.html<\/strong><\/p>\n<pre class=\"brush: xml; gutter: false; title: ; notranslate\" title=\"\">\r\n&lt;html&gt;\r\n  &lt;head&gt;\r\n    &lt;script src=&quot;script.js&quot; defer&gt;&lt;\/script&gt;\r\n  &lt;\/head&gt;\r\n  &lt;body&gt;\r\n    &lt;table&gt;\r\n      &lt;tr&gt;&lt;td&gt;Input&lt;\/td&gt;&lt;td&gt;&lt;select id=&#039;input&#039;&gt;&lt;\/select&gt;&lt;\/td&gt;&lt;\/tr&gt;\r\n      &lt;tr&gt;&lt;td&gt;Output&lt;\/td&gt;&lt;td&gt;&lt;select id=&#039;output&#039;&gt;&lt;\/select&gt;&lt;\/td&gt;&lt;\/tr&gt;\r\n    &lt;\/table&gt;\r\n    &lt;audio id=&#039;player&#039; autoplay&gt;&lt;\/audio&gt;\r\n  &lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<hr>\n<p><strong>script.js<\/strong><\/p>\n<pre class=\"brush: jscript; gutter: false; title: ; notranslate\" title=\"\">\r\nconst input = document.getElementById(&#039;input&#039;);\r\nconst output = document.getElementById(&#039;output&#039;);\r\nconst player = document.getElementById(&#039;player&#039;);\r\n\r\nfunction setStream(stream) {\r\n  player.srcObject = stream;\r\n};\r\n\r\nfunction getDevices() {\r\n  return navigator.mediaDevices.enumerateDevices();\r\n}\r\n\r\nfunction fillLists(devices) {\r\n  devices.forEach(function(device) {\r\n    const option = document.createElement(&#039;option&#039;);\r\n    option.value = device.deviceId;\r\n\r\n    if (device.kind === &#039;audioinput&#039;) {\r\n      option.text = (device.label ||\r\n                    &#039;Audio Input Device &#039; + input.length);\r\n      input.appendChild(option);\r\n    }\r\n    else if (device.kind === &#039;audiooutput&#039;) {\r\n      option.text = (device.label ||\r\n                    &#039;Audio Output Device &#039; + output.length);\r\n      output.appendChild(option);\r\n    }\r\n  });\r\n}\r\n\r\nfunction outputChanged() {\r\n  player.setSinkId(output.value ? output.value : &#039;default&#039;);\r\n}\r\n\r\nfunction inputChanged() {\r\n  const constraint = {\r\n    audio: { deviceId: input.value ? input.value : &#039;default&#039; }\r\n  };\r\n  navigator.mediaDevices.getUserMedia(constraint)\r\n    .then(setStream);\r\n}\r\n\r\nfunction initalize() {\r\n  navigator.mediaDevices.getUserMedia({ audio: true })\r\n    .then(setStream).then(getDevices).then(fillLists);\r\n}\r\n\r\noutput.onchange = outputChanged;\r\ninput.onchange = inputChanged;\r\n\r\ninitalize();\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This example records from the selected audio input device (microphone) and plays to the selected audio output device (speaker). Be<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false},"categories":[55],"tags":[],"_links":{"self":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/13415"}],"collection":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=13415"}],"version-history":[{"count":1,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/13415\/revisions"}],"predecessor-version":[{"id":16834,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/13415\/revisions\/16834"}],"wp:attachment":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=13415"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=13415"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=13415"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}