This is a mirror of official site: http://jasper-net.blogspot.com/

The ABC of streaming in Silverlight

| Monday, June 6, 2011
Advanced media integration is one of Silverlight’s greatest strengths. In this tutorial we delve into this topic by carrying out some simple exercises. We will learn how to play a movie, how to interact with webcam and microphone and how to create a live streaming solution using Expression Encoder 4 and VLC and capture it in a Silverlight application.

Play a movie
In Silverlight there is a native control called MediaElement which allows you to do a lot of things. The first thing obviously is the possibility to play a video: using its Source property you can define the location of the video you want to see and you can use either a relative or absolute URL. Suppose you have a collection of videos in a subdirectory called “Videos” of your web application and you want to play one of them. The xaml code snippet below is all you will need:

<Grid x:Name="LayoutRoot" >
<MediaElement  x:Name="mediaEl" Source="videos/video1.wmv" />
</Grid>

If you launch the application the movie will start automatically. This happens because the Autostart property of the MediaElement is true by default. To control the video, the MediaElement exposes the canonical Play(), Pause() and Stop() methods. At this point adding a commandbar to control the video is fairly easy ; just add a StackPanel in the xaml as below:

<StackPanel Name="CommandBar" Orientation="Horizontal" Background="Beige" Height="39" Width="783" Margin="5" HorizontalAlignment="Center" Canvas.Left="3">
<Button Name="PlayMovie" Background="AntiqueWhite" Content="Play" FontWeight="Bold" Click="PlayMovie_Click" Margin="50,5,0,5" Width="100"/>
<Button Name="PauseMovie" Background="AntiqueWhite" Content="Pause" FontWeight="Bold" Click="PauseMovie_Click" Margin="50,5,0,5" Width="100"/>
<Button Name="VideoStop" Background="AntiqueWhite" Content="Stop" FontWeight="Bold" Click="VideoStop_Click" Margin="50,5,0,5" Width="100" />
</StackPanel>
 

And, in the code behind, the following event handlers:

private void PlayMovie_Click(object sender, RoutedEventArgs e)
        {
            if(mediaEl.CurrentState != MediaElementState.Opening ||
                mediaEl.CurrentState == MediaElementState.Stopped ||
                mediaEl.CurrentState == MediaElementState.Paused)
                mediaEl.Play();
 
        }
 
        private void PauseMovie_Click(object sender, RoutedEventArgs e)
        {
            if (mediaEl.CurrentState == MediaElementState.Playing)
                mediaEl.Pause();
 
        }
 
        private void VideoStop_Click(object sender, RoutedEventArgs e)
        {
            if(mediaEl.CurrentState == MediaElementState.Playing ||
                mediaEl.CurrentState == MediaElementState.Paused)
                mediaEl.Stop();
 
        }
 

Let’s focus on the video formats supported. The list below recaps all the formats recognized by Silverlight 4:

Read more: Silverlight Show

Posted via email from Jasper-net

0 comments: