Input Device Selection
Unreal does not provide an easy method to switch input devices (like a microphone) when using the default Audio Capture, even though most players would expect this feature in a game with Voice Chat. Let’s take a look at how we handle this in the ODIN plugin.
By David Liebemann at SciCode Studio.
Creating an Odin Audio Capture
To change input devices, instead of using the default
Create Audio Capture method, we’ll use the
Create Odin Audio Capture function call. This will create an extended Audio Capture object, that allows us to change the audio capture device using either Blueprints or C++.
Your logic for joining an Odin room should now look something like this:
Please note, that we store a reference to the
Odin Audio Capture object as a variable - we’re going to use it in the next sections to display the available devices in the UI.
You’ll probably have to adjust the next steps to fit your UI implementation, but in general it’s easy to adapt the necessary UI elements.
We’ll create a new Blueprint Widget that gets displayed when the player presses `tab. This Widget should show the available capture devices in a drop-down box and change the active device on selection.
- Create a new Widget Blueprint in your project - we’ll call it
- In the widget’s event graph, create a new Custom Event called
UpdateAudioCapturewith an input parameter of type
Odin Audio Capture. We’ll use this event in the next section, so simply leave it unconnected for now.
In your player controller’s
Begin Play, add a
Create Widgetcall, select the class
BP_SelectInputDevice, connect the
Selfproperty to the
Owning Playerinput and save the resulting widget object in a variable.
The final step in the player controller is to add logic for toggling the “Select Input Device” UI, as shown below. Please note the call to
Update Audio Capturewhen making the UI visible. This will supply the
Audio Captureobject to the widget blueprint, which we’ll use to request the available capture devices.
If you can’t create the
InputAction ToggleDeviceSelection event, make sure to add an Action in
Project Settings > Input > Action Mappings and bind it to a keyboard input - we’re using Tab for testing.
Currently the widget does not contain any visible UI elements, so let’s change this. We’ll add a simple see-through background and a dropdown box, in which we’ll list all available capture devices. Changing the selection in the dropdown box will change the capture device used for Odin. Additionally we’ll preselect the currently active capture device in the dropdown box.
In the widget’s
Designermode, add a
Canvas Panelas the root element and a
ComboBox (String)as direct children. Set the
(0,0,0,0.5)to make it darken the gameplay, but still keep it see-through. Additionally, let’s give the
ComboBoxa meaningful name - e.g. “SelectionDropBox”.
Let’s now switch to the
Graphmode and start extending our Custom Event
UpdateAudioCapture. We’ll store the
Odin Audio Captureobject as a variable and call
Async Get Capture Devices Available. This will return the available capture devices and the current device as outputs in the connected event.
Odin Audio Capture object also contains synchronous function calls to access and change capture device data. During testing we’ve noticed that these synchronous calls can take quite a while to run: more than 100ms on average, in some cases more than a second. This lag is due to Unreal’s internal implementation for changing capture devices. We therefore strongly recommend using the async function calls.
Let’s name the connected event
OnReceivedDevices. We’ll store a reference to the available devices, as well as retrieve and store the current device name from the
Current DeviceInfo structures.
OnReceivedDeviceswill be called each time we open up the UI, we’ll need to reset the current options by calling
Clear Optionson the
Selection Drop Box.
Now we need to add the available capture device names as options in the dropdown box. We iterate over our
Available Devicesarray, retrieve the
Device Namefrom the device info structures and add that as an option to the
Selection Drop Box.
Afterwards we’ll preselect the currently active device by calling
Set Selected Optionwith our
Current Option. Your blueprint logic should look something like this:
Finally we’ll change the capture device, when the user selects an item from the dropdown box. Add the event
On Selection Changed (SelectionDropBox)to your Event Graph. The
Selected Itemwill contain the name of the input device that should become the active capture device. Let’s do a quick check for whether the
Selected Itemis empty or equal to the currently selected option. If the item passes the checks, we call
Async Change Capture Device by Namewith the
Selected Itemas input. The
On Change Completedcallback will let us know, whether the given device was activated successfully. Your blueprint should look something like this:F
Now anytime a user chooses a new option, the
Odin Audio Capture object will change the capture device and restart the input stream automatically.
This is a very simple implementation with minimal UI, which you’ll be able to easily adapt to your project’s needs. For more information on Unreal with Odin Voice Chat, check out our Discord and take a look at the following guides we’ve prepared for you: