How to do Webcam and Microphone Integration in Silverlight 4

Here I am going to take you through an example to understand how to do webcam and microphone integration in Silverlight 4.

One of the widely demanded features, web camera and microphone integration, is finally introduced in Silverlight 4, with the introduction of a new set of APIs to identify the attached audio and video devices to the computer, and play and capture the audio and video. It also supports capturing raw audio and video and processing using VideoSink and AudioSink classes, which can eventually begin a new era of empowering developers to create innovative media-driven applications, including live audio and video conferencing. Reference: Google Books
Here I am going to take you through a beautiful example to understand this:
Step 1: Create a new Silverlight project.
Add the following code to the XAML page:

<UserControl x:Class="SilverlightApp_webcamMicrophone.MainPage"
    d:DesignHeight="300" d:DesignWidth="400">
    <Grid x:Name="LayoutRoot" Background="#333333">
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <RowDefinition Height="240*"/>
            <RowDefinition Height="60*"/>
        <Rectangle x:Name="CaptureDisplay"
        <Button x:Name="StartCapture"
                Content="Start Capture"
                Grid.Row="1" Grid.Column="0"
        <Button x:Name="StopCapture"
                Content="Stop Capture"

                           Grid.Row="1" Grid.Column="1"

Then in the Xaml.cs page add the following code which is self-explanatory:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SilverlightApp_webcamMicrophone
    public partial class MainPage : UserControl
        CaptureSource _captureSource;
        public MainPage()
   private void StartCapture_Click(object sender, RoutedEventArgs e)
      //if (CaptureDeviceConfiguration.AllowedDeviceAccess ||
      //    CaptureDeviceConfiguration.RequestDeviceAccess())
      //    // It's always safe to call Stop(), even if no capture is running.
      //    // However, attempting to call Start() while a capture is already running,
      //    // without calling Stop() first, causes an exception.
      //    _captureSource.Stop();
      //    // Get the default webcam.
      //    _captureSource.VideoCaptureDevice =
      //    CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice();
      //    if (_captureSource.VideoCaptureDevice == null)
      //    {
      //        MessageBox.Show("Your computer does not have a video capture device.");
      //    }
      //    else
      //    {
      //       // Start a new capture.
      //       _captureSource.Start();
      //       // Map the live video to a VideoBrush.
      //       VideoBrush videoBrush = new VideoBrush();
      //       videoBrush.Stretch = Stretch.Uniform;
      //       videoBrush.SetSource(_captureSource);
      //       // Use the VideoBrush to paint the fill of a Rectangle.
      //       CaptureDisplay.Fill = videoBrush;
      //    }
            if (_captureSource != null)
            _captureSource = new CaptureSource();
            _captureSource.VideoCaptureDevice = CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice();
            _captureSource.AudioCaptureDevice = CaptureDeviceConfiguration.GetDefaultAudioCaptureDevice();
            // create the brush
            VideoBrush vidBrush = new VideoBrush();
            CaptureDisplay.Fill = vidBrush; // paint the brush on the rectangle
            // request user permission and display the capture
            if (CaptureDeviceConfiguration.AllowedDeviceAccess || CaptureDeviceConfiguration.RequestDeviceAccess())
        private void StopCapture_Click(object sender, RoutedEventArgs e)