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"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
    <Grid x:Name="LayoutRoot" Background="#333333">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="240*"/>
            <RowDefinition Height="60*"/>
        </Grid.RowDefinitions>
        <Rectangle x:Name="CaptureDisplay"
                   Fill="White"
                   Grid.ColumnSpan="2"
                   Margin="5"/>
        <Button x:Name="StartCapture"
                Click="StartCapture_Click"
                Content="Start Capture"
                Grid.Row="1" Grid.Column="0"
                Margin="5"/>
        <Button x:Name="StopCapture"
                Click="StopCapture_Click"
                Content="Stop Capture"

                           Grid.Row="1" Grid.Column="1"
                Margin="5"/>
    </Grid>
</UserControl>

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()
        {
            InitializeComponent();
        } 
       
   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.Stop();
            }
            _captureSource = new CaptureSource();
            _captureSource.VideoCaptureDevice = CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice();
            _captureSource.AudioCaptureDevice = CaptureDeviceConfiguration.GetDefaultAudioCaptureDevice();
            // create the brush
            VideoBrush vidBrush = new VideoBrush();
            vidBrush.SetSource(_captureSource);
            CaptureDisplay.Fill = vidBrush; // paint the brush on the rectangle
 
            // request user permission and display the capture
            if (CaptureDeviceConfiguration.AllowedDeviceAccess || CaptureDeviceConfiguration.RequestDeviceAccess())
            { 
                _captureSource.Start();
            }
        }
 
        private void StopCapture_Click(object sender, RoutedEventArgs e)
        {
            _captureSource.Stop();
        }
    }
}

Up Next
    Ebook Download
    View all
    Learn
    View all