<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Austin Kurpuis on Medium]]></title>
        <description><![CDATA[Stories by Austin Kurpuis on Medium]]></description>
        <link>https://medium.com/@austinkurpuis?source=rss-76de46c698f5------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*fRwhXIVBkhFbtdbC.jpg</url>
            <title>Stories by Austin Kurpuis on Medium</title>
            <link>https://medium.com/@austinkurpuis?source=rss-76de46c698f5------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 13:09:16 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@austinkurpuis/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Making a User Media Preview Component]]></title>
            <link>https://medium.com/the-js-guys/making-a-user-media-preview-component-8571d02b69e3?source=rss-76de46c698f5------2</link>
            <guid isPermaLink="false">https://medium.com/p/8571d02b69e3</guid>
            <category><![CDATA[mediastream]]></category>
            <category><![CDATA[media-devices]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[webrtc]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Austin Kurpuis]]></dc:creator>
            <pubDate>Tue, 07 Aug 2018 07:00:26 GMT</pubDate>
            <atom:updated>2018-08-07T07:12:31.444Z</atom:updated>
            <content:encoded><![CDATA[<p>I recently had the need to build a User Media Preview component for a client’s application.</p><p>Essentially, this component displays a list of audio and video sources and allows the user to choose from the lists and preview the selection instantly. Then, a property on the component allows a consumer to get a <a href="https://developer.mozilla.org/en-US/docs/Web/API/MediaStreamConstraints">media constraints</a> object that can be passed to <a href="https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia">getUserMedia</a> or a WebRTC framework, like <a href="https://github.com/meetecho/janus-gateway">Janus</a>, for example.</p><p>In this article, I will run through the steps to create this component using <a href="https://donejs.com/">DoneJS</a> and <a href="https://canjs.com/">CanJS</a>, but the fundamental concepts I will explain in this article can be applied to just about any framework you want to use, or even plain JavaScript if that’s your jam. Alternatively, you can simply use the <a href="https://www.npmjs.com/package/donejs-user-media-selector">DoneJS plugin</a> I created for this article.</p><h4>Introduction to the MediaDevices APIs</h4><p>Before we get started, we should talk about a couple of browser APIs, <a href="https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/enumerateDevices">enumerateDevices</a>, <a href="https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia">getUserMedia</a>, and the <a href="https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/ondevicechange">devicechange</a> event.</p><p>First off: <a href="https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/enumerateDevices">enumerateDevices</a> returns a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise">Promise</a> that resolves a list of available media devices connected to the computer, such as built in or USB connected cameras, microphones and speakers.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/56018177c3d0904aba59118ce83e6268/href">https://medium.com/media/56018177c3d0904aba59118ce83e6268/href</a></iframe><p>Note that the label property will always be an empty string if the user hasn’t granted access to their media yet, this is for security purposes.</p><p>Running the above code in a browser will output something like:</p><pre>audioinput: Default - Internal Microphone (Built-in)<br>audioinput: Internal Microphone (Built-in)<br>videoinput: FaceTime HD Camera<br>audiooutput: Default - Internal Speakers (Built-in)<br>audiooutput: Internal Speakers (Built-in)</pre><p>We can use this to populate the UI with a list of available input options.</p><p>Next up: <a href="https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/ondevicechange">devicechange</a> is an event we can listen for on the <a href="https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices">navigator.mediaDevices</a> object. It will be fired when the available user media changes, like when the user connects a new webcam or disconnects an existing one.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fda43e3cc490a44320991a8bd7594bf5/href">https://medium.com/media/fda43e3cc490a44320991a8bd7594bf5/href</a></iframe><p>The above code will log Changed! when a device is added or removed. If you plugin a device that has two sources, like a webcam with a built-in mic, this even will fire twice. We can use this to know when our device list should be updated without polling.</p><p>Finally: <a href="https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia">getUserMedia</a> takes a media constraints object and returns a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise">Promise</a> that resolves an instance of the <a href="https://developer.mozilla.org/en-US/docs/Web/API/MediaStream">MediaStream</a> constructor that represents whatever user media that was specified in the constraints.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/585f7e366adeeaf0c043b9360f61a9b1/href">https://medium.com/media/585f7e366adeeaf0c043b9360f61a9b1/href</a></iframe><p>Note that once the user gives permission to use their media devices, the webcam light will turn on indicating it is in use.</p><p>We can attach the resolved stream to a &lt;video&gt; tag to display the selected device’s media content to the user.</p><h4>Putting It All Together</h4><p>We’re going to start out by making a new <a href="https://canjs.com/">CanJS</a> component, you can use the <a href="https://donejs.com/Apis.html#cli-and-generators">DoneJS CLI</a> to scaffold a basic component for you.</p><p>To start out, my JavaScript file looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d572bd643d2ad77dba59e7531a1c21c8/href">https://medium.com/media/d572bd643d2ad77dba59e7531a1c21c8/href</a></iframe><p>We’ll take a look at the .stache and .less files later. We’re going to build the view model first. Let’s start by adding a <a href="https://canjs.com/doc/can-component/connectedCallback.html">connectedCallback</a> method. This is a lifecycle hook built into <a href="https://canjs.com/doc/can-component.html">can-component</a>. It is’s called when the component is inserted into the DOM and may return a teardown function that will be called when the component is removed from the DOM.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/29c1e3b9dd8040da47662313e4f597fe/href">https://medium.com/media/29c1e3b9dd8040da47662313e4f597fe/href</a></iframe><p>On line 5 through 9, we create a function called deviceChangeHandler that gets a list of devices and attached them to the devices property on the view model, we immediately call it to initialize this property.</p><p>On line 12, we setup an event listener on <a href="https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/ondevicechange">devicechange</a> and provide our deviceChangeHandler function as a handler.</p><p>On line 14, we setup an event listener for constraints on the view model instance with the <a href="https://canjs.com/doc/can-event-queue/map/map.listenTo.html">listenTo</a> method. This will fire anytime the constraints property changes, its handler is using the <a href="https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia">getUserMedia</a> API to get a MediaStream with the provided constraints and sets it to the previewStream property on the view model. Note we haven’t added a constraints property just yet.</p><p>Finally, on line 22, we return a function that when called unbinds our event listeners.</p><p>Now we’re going to need to add some properties and getters.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/428b166cceed0c9a8ee6df1516763686/href">https://medium.com/media/428b166cceed0c9a8ee6df1516763686/href</a></iframe><p>Most of these properties and getters are pretty simple and self explanatory so I won’t go into too much detail, but there are a couple things that are worth mentioning.</p><p>On line 12, the constraints getter will build a constraints object using the selectedVideoDevice and selectedAudioDevice properties and return it. The cool thing about this is that we can listen for changes to constraints and if any of it’s dependencies change, the event will fire with the new computed value of constraints. This means when we update selectedVideoDevice or selectedAudioDevice, our listener on line 85 will fire. This magic is built into DefineMap, plain objects do not work like this.</p><p>On line 40 and 44, you’ll see getters for audio and video devices, they filter the devices property and return a list of applicable devices. Notice they also take the first item in the list and set it to the selectedVideoDevice or selectedAudioDevice accordingly, this will make our component default to the first available device.</p><p>Now that we’ve created our view model, we’re going to move on to the view, we’ll write this in our .stache file.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/67c03efcbd4c04adc2f85240ea31a571/href">https://medium.com/media/67c03efcbd4c04adc2f85240ea31a571/href</a></iframe><p>On line 1, we setup and binding from the previewStream property on the view model to the <a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/srcObject">srcObject</a> property on the &lt;video&gt; element. This means when previewStream is set, <a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/srcObject">srcObject</a> property on the element will also be set.</p><p>On line 4, we setup a two-way binding from the value of the &lt;select&gt; element to the selectedVideoDevices property on the view model. This means when we select an option from the list in the UI, the value will be set to the selectedVideoDevices property on the view model. Also, when we set the selectedVideoDevices property in the view model, the &lt;select&gt; will be set to this value.</p><p>On line 5 through 7, we use the <a href="https://canjs.com/doc/can-stache.helpers.each.html">each</a> helper in <a href="https://canjs.com/doc/can-stache.html">can-stache</a> to iterate through our list of selectedVideoDevices render an &lt;option&gt; for each item. This list will also automatically re-render if selectedVideoDevices changes. On line 13 through 15, we repeat this process for selectedAudioDevices.</p><p>Finally, we’re going to add some basic styles in our .less file just so the component isn’t too terribly ugly. This article isn’t about styling or less, and I am not a designer or CSS expert, so I’m not going to explain this.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/715b3237ced3e84f485ec391a1af3e0e/href">https://medium.com/media/715b3237ced3e84f485ec391a1af3e0e/href</a></iframe><p>At this point, we should be able to test out our component and see it working. I made a simple demo page you can steal to make testing this super easy. Just drop the following snippet in an HTML file and serve it up from your favorite dev server, I use <a href="https://www.npmjs.com/package/http-server">http-server</a>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9fd237a199d11888454117ed0c14a7e2/href">https://medium.com/media/9fd237a199d11888454117ed0c14a7e2/href</a></iframe><p>And bam! Our media selector is working! Note that you’ll need StealJS installed in your project for this demo to work.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5_DXktbx9DUx13Kjs-9h5w.png" /></figure><h4>Wrapping things up</h4><p>I hope you found this article helpful. If you have any questions or see room for improvements in this article, please feel free to comment or reach out to me on Twitter <a href="https://twitter.com/imaustink">@imaustink</a>.</p><p>Again, if you are feeling lazy or just in a hurry, go ahead and <a href="https://gitlab.com/thejsguys/donejs-user-media-selector">clone/fork my media selector on GitLab</a> or <a href="https://www.npmjs.com/package/donejs-user-media-selector">install it via npm</a>.</p><p>Some of the topics I am considering covering in my next article are:</p><ul><li>Adding an audio meter to our preview</li><li>Integrating our preview into a WebRTC app</li><li>Integrating our preview into an Electron webcam recording app</li></ul><p>Let me know in the comments what you’d like me to write about first!</p><p>If this article was informative or helpful, please give it a few claps, and thank you very much for reading it to the end!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8571d02b69e3" width="1" height="1" alt=""><hr><p><a href="https://medium.com/the-js-guys/making-a-user-media-preview-component-8571d02b69e3">Making a User Media Preview Component</a> was originally published in <a href="https://medium.com/the-js-guys">The JS Guys</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>