Select media

Load Library

It is possible to choose a media to use (specific microphone or camera), and ApiRTC provides helper functions for that. The following code sample allows a user to select his preferred camera and microphone. His choice is also remembered, should he refresh the page.

HTML

Javascript

You notice that most of the code and event handlers was already present in the second audio/video example. Selecting a media shows a complete example of how to interact with ApiRTC in order to customize it’s behavior.

The first interesting snippet is the request made to ApiRTC to get the list of the available media devices (microphones and cameras).

This line is called when ApiRTC is initialized, and will have as parameters the audio and video sources available to ApiRTC, exposed by the webRTCClient.
In the gotSource function that we defined, the different options are then displayed in the selectors, so that the user can choose it’s microphone and camera. His choice is also retrieved from the localStorage if there was one.

When the user clicks one of the selectors, his new choice is also stored in the localStorage. The consequence is that the user preference is persistent after a page refresh. When the user changes it’s choice, the information has to be forwarded to ApiRTC, so that the preferred camera and microphone will be used.

This is the purpose of the following function call (for the audio). We also store it in the localStorage.

The audioSourceIdInLocalStorage variable must be one of the Ids sent as parameters from the getMediaDevices function (the gotSources parameter) function. This way, during the subsequent calls, ApiRTC will use the specified device. There is a similar mechanism to choose the video source using the setVideoSourceId function.

The last step is to instruct ApiRTC to use a particular device. This is done via selectMediaChange() function.