Discovering Devices
Discovering Media Devices
The very first step in using media devices is to discover available devices. To do so, you will use the
MediaDeviceQuery
.The
MediaDeviceQuery
class is responsible for finding and reporting available media devices. To begin, simply create one:// Create a device query
var query = new MediaDeviceQuery();
Behind the scenes, the device query will inspect the system you are running on and find all available media devices. It will then provide all these devices as implementations of the
IMediaDevice
interface.// We can now look through devices
foreach (IMediaDevice device in query)
...
All media devices implement the
IMediaDevice
interface. To use more specific functionality, perform a type cast to the media device type you need.The device query allows for only finding devices that meet certain conditions. This is useful for filtering for specific types of devices (like camera vs. microphone) or finding devices that support certain features (like echo-cancelling microphones). To filter devices, simply pass a predicate to the
MediaDeviceQuery
constructor:// Find only cameras
var query = new MediaDeviceQuery(
device => device is CameraDevice
);
// Find only cameras
var filter = MediaDeviceCriteria.CameraDevice;
var query = new MediaDeviceQuery(filter);
There are cases where you need to loop through available devices over a long period of time. A good example of this is in camera apps that allow the user to switch cameras. In such cases, the
MediaDeviceQuery
can be used as a cursor for enumerating through the devices it has discovered. The query provides the current
property which returns the device at the current cursor position:// Create a device query
var query = new MediaDeviceQuery();
// Then get the device at the current cursor position
var device = query.current;
The cursor can be advanced when necessary:
// Advance the cursor
query.Advance();
// And now we can use the next device
var device = query.current;
The query will loop back to the first device when it has been advanced to its last device.
Last modified 1yr ago