Silverlight 4: List Audio and Video Devices

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:

Silverlight Audio Video

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:

Silverlight Audio Video Permission

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:

Silverlight Audio Video

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:

Silverlight Audio Video

Now run the application and click the button and you should be able to see the Audio and Video devices on your machine

Silverlight Audio Video

In a forthcoming article, we will see how to extend this example and capture video and audio using Silverlight



Will you give this article a +1 ? Thanks in advance


About The Author

Suprotim Agarwal
Suprotim Agarwal, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook

Follow him on twitter @suprotimagarwal

No comments: