In this post, we will see how to use Silverlight to list the available audio and video devices on your system. We will list this information into separate ComboBoxes.
The CaptureDeviceConfiguration Class is a helper class for obtaining information about available capture devices (audio or video) and requesting client user permission to access the captures from available devices. We will be using the following 3 important methods of this class
RequestDeviceAccess – This method requests access for all audio or video devices available on the system.
GetAvailableAudioCaptureDevices - Returns a collection of AudioCaptureDevice objects
GetAvailableVideoCaptureDevices - Returns a collection of VideoCaptureDevice objects
Let’s see some code. First design a layout with two TextBlock, two ListBox and a Button control, as shown below:
In the btnFetch_Click event, add the following code:
private void btnFetch_Click(object sender, RoutedEventArgs e)
{
// First request access
if (CaptureDeviceConfiguration.RequestDeviceAccess()
CaptureDeviceConfiguration.AllowedDeviceAccess)
{
// Bind the
lbAudio.ItemsSource =
CaptureDeviceConfiguration.GetAvailableAudioCaptureDevices();
lbVideo.ItemsSource =
CaptureDeviceConfiguration.GetAvailableVideoCaptureDevices();
}
}
Run the application and click the button. You should see a dialog similar to the one shown below:
The CaptureDeviceConfiguration.AllowedDeviceAccess property checks whether the Silverlight application has permission to access the video and audio devices. If this property returns false, the RequestDeviceAccess() is called and you see this dialog. You can save the permission by checking the box ‘Remember my answer’ and click Yes.
You will see the following output:
As you can see, the device names do not show up. In order to display the name of the device, you’ll need to add an item template to the ListBox which displays the FriendlyName property of each audio and video device. Go back to your XAML code and make the following changes:
1. Add a DataTemplate to the UserControl.Resources as shown below:
<UserControl.Resources>
<DataTemplate x:Key="MediaTemplate">
<TextBlock Text="{Binding FriendlyName}" />
</DataTemplate>
</UserControl.Resources>
2. Now use this Template in both the ListBox as shown below:
<ListBox x:Name="lbAudio" Grid.Column="0" Grid.Row="1"
ItemTemplate="{StaticResource MediaTemplate}">
</ListBox>
Once the changes have been made, the entire XAML will look similar to one shown below:
Now run the application and click the button and you should be able to see the Audio and Video devices on your machine
In a forthcoming article, we will see how to extend this example and capture video and audio using Silverlight
Tweet
No comments:
Post a Comment