DCSIMG
Silverlight 4: New features overview (Part 1) – Webcam/Mic Support - Alex Golesh's Blog About Silverlight Development

Silverlight 4: New features overview (Part 1) – Webcam/Mic Support

This post starts a series of post about new features in Silverlight 4 Beta released at PDC 09.

Today I’ll show how to use Webcam/Mic support feature in the new version.

In order to use the Webcam/Mic the user should has valid drivers installed. To see which from installed drivers could be used open Silverlight Configuration and select 4th tab:

image

This tab also allows to select default webcam/mic to use with Silverlight applications.

Webcam/Mic controlled by CaptureSource class. This class allows to select which capture device to use (Video and Audio) and controls capturing.

In order to demonstrate the API I’ve created sample applciation. It will looks like follows:

image

Let’s see how to operate “Webcam On” button…

I’ve created class variable:

CaptureSource cs;

 

This variable will be used also for the second button handler, that’s why I’ve created it in class level.

In handler function, in order to use webcam/mic we have to get the end-user permission:

if (CaptureDeviceConfiguration.AllowedDeviceAccess ||
         CaptureDeviceConfiguration.RequestDeviceAccess())
{
...
}

Those lines in runtime will render the standard Silverlight dialog box with a request to use webcam/mic. If user approves the request we will be able to use those devices:

image

In my sample I’ll use default video device, and will set it as a video capture source for my capture sources:

VideoCaptureDevice vcd = CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice();
//AudioCaptureDevice acd = CaptureDeviceConfiguration.GetDefaultAudioCaptureDevice(); //Could use it, but not needed heere
if (null != vcd /*&& null != acd*/)
{
   cs = new CaptureSource();
   cs.VideoCaptureDevice = vcd;
   //cs.AudioCaptureDevice = acd;
...

When everything is set I’ll start capturing.

cs.Start();

I’ll use the capture source as a source for the videobrush, and will paint with it the rectangle:

VideoBrush videoBrush = new VideoBrush();
videoBrush.Stretch = Stretch.Uniform;
videoBrush.SetSource(cs);
rectFill.Fill = videoBrush;

When running the application it will capture the video from the webcam and display it on screen:

image

Now for the capture of image. The handler of the “Capture Me!” button will enable asynchronous capturing of the image, and after getting the captured image will popup ChildWindow. The received image will be as a source for an image on this popup.

Button handler:

cs.AsyncCaptureImage(GetImage);

The “GetImage” function body (ScreenCapture is a ChildWindow instance):

ScreenCapture sc = new ScreenCapture();
sc.img.Source = bitmap;
sc.Show();

The running application:

image

 

The source for this app here (need Visual Studio 2010 Beta 2 & SL4 Beta).

 

Next time I’ll show how to use new built-in print support to print the captured image.

 

Stay tuned,

Alex

Published Wednesday, November 18, 2009 9:16 PM by Alex Golesh

Comments

# re: Silverlight 4: New features overview (Part 1) – Webcam/Mic Support

Whats the best way to have a video-chat application between 2 clients (am hoping this would be faster than MSN and the like, hence this Question)--- Is it to have each client connect to other client's webserver(IPAddress?)

Thanks

Monday, November 23, 2009 9:47 PM by Sriram

# Silverlight 4: New features overview (Part 1) ??? Webcam/Mic Support … Silverlight Web

Pingback from  Silverlight 4: New features overview (Part 1) ??? Webcam/Mic Support … Silverlight Web

# Silverlight 4: New features overview (Part 1) ??? Webcam/Mic Support … Silverlight Blog

Pingback from  Silverlight 4: New features overview (Part 1) ??? Webcam/Mic Support … Silverlight Blog

# Silverlight 4: New features overview (Part 1) ??? Webcam/Mic Support … iSilverlight

Pingback from  Silverlight 4: New features overview (Part 1) ??? Webcam/Mic Support … iSilverlight

# re: Silverlight 4: New features overview (Part 1) – Webcam/Mic Support

Is it possible to save audio file recorded to IIS Server?

Tuesday, December 01, 2009 7:15 AM by frank

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: