Building your first Smooth Streaming Player using SSME

In this post I plan to cover building a very basic Smooth Streaming Player using Smooth Streaming Media Element (SSME) part of Smooth Streaming Player Development Kit. In other words, building a “Hello World” equivalent for Smooth Streaming Client (“Hello Smooth Streaming”). This post is split into two parts. In the first part, I will be covering how to get basic playback working in a browser window for Smooth Streaming content. This should be really easy as you will see. In the second part, we would add some basic playback controls like Play, Pause, Stop and Volume controls.


Prerequisites



  1. You should either have Visual Web Developer or Visual Studio installed along with Silverlight Tools for Visual Studio. You can get more information on this here.

  2. Install the Smooth Streaming Developer Kit available here.

  3. This is not a XAML overview session so basic XAML knowledge is required.

Building a Simple Smooth Streaming Player for Playback


The very first thing I wanted to cover here was just basic playback. There are no controls. All you get is a browser window that shows the video playing.


The below outlines steps to achieve this:



  1. Open Visual Web Developer or Visual Studio.

  2. Go to: File –> New Project.

  3. Chose Visual C# as project Type and then select Silverlight. Choose Silverlight Application, name it SimplePlayer.

  4. In your project go to references and add a reference to Microsoft.Web.Media.Smoothstreaming.dll (available as a part of Player development Kit)

  5. Modify MainPage.xaml as under. Set the SmoothStreamingSource property in XAML to the location of the Smooth Streaming content. Replace <> with the URL of the content (E.g., it will look something like http://example.com/a.isml/Manifest)



Code Snippet



  1. <UserControl x:Class="SimplePlayer.MainPage"

  2.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

  3.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

  4.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

  5.     xmlns:SSME="clr-namespace:Microsoft.Web.Media.SmoothStreaming;assembly=Microsoft.Web.Media.SmoothStreaming"

  6.     mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">

  7.   <Grid x:Name="LayoutRoot">

  8.     <SSME:SmoothStreamingMediaElement AutoPlay="True" x:Name="SmoothPlayer" SmoothStreamingSource="<>" Grid.Row="0" />

  9.   Grid>

  10. UserControl>


This is all you need to get a basic playback working with Smooth Streaming Media Element.


Adding Playback Controls to your Player


Next, once you have the basic playback working, you may want to add some basic controls like Play/Pause button, Stop Button and Volume Control.


Here is what you will see after completing the steps below:


image


The UI layout


Here are the pieces we need for this in terms of the layout.



  1. Have portion of the screen, which will display the slider control for volume and Play/Pause and Stop buttons

  2. We need a portion for the Video to display

  3. We need to hook these buttons/slider to SSME

For the overall layout, we are using XAML constructs of Stack Panels. here is how we do it in this example:


 




Code Snippet



  1. <UserControl x:Class="SimplePlayer.MainPage"

  2.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

  3.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

  4.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

  5.     xmlns:SSME="clr-namespace:Microsoft.Web.Media.SmoothStreaming;assembly=Microsoft.Web.Media.SmoothStreaming"

  6.     mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">

  7.   <Grid x:Name="LayoutRoot">

  8.         <Grid.RowDefinitions>

  9.             <RowDefinition Height="0.95*">RowDefinition>

  10.             <RowDefinition Height="0.05*">RowDefinition>

  11.         Grid.RowDefinitions>

  12.         <SSME:SmoothStreamingMediaElement AutoPlay="True" x:Name="SmoothPlayer" SmoothStreamingSource="<>" Grid.Row="0" />

  13.         <StackPanel Orientation="Horizontal" Grid.Row="1">

  14.             <TextBlock x:Name="Volume" VerticalAlignment="Center" Text="Volume" Width="50" />

  15.             <Slider x:Name="VolumeBar" Width="60" Value="{Binding Path=Volume, ElementName=SmoothPlayer, Mode=TwoWay}" />

  16.             <Button x:Name="PlayButton" Width="50" Click="PlayButton_Click" Loaded="PlayButton_Loaded"/>

  17.             <Button x:Name="StopButton" Content="Stop" Width="50" Click="StopButton_Click" />

  18.         StackPanel>

  19.     Grid>

  20. UserControl>


 


 


 


We have a Grid with two rows: the first row is SSME and second is a horizontal Stack Panel that has volume slide and Pause / Stop buttons. This is all we need to get the basic layout.


Linking UI to SSME


The next step after getting the UI is to link it back to SSME. We already know how to play the video in SSME. In this section I would cover how to hook up the buttons and the slider.


Silverlight has a concept of template binding that can be used here. In the case of slider, we have a slider volume UI control and a volume property on SSME. All we need to do here is to tell Silverlight via the XAML to bind the slider value to the volume property on SSME. Here is how we do it:




Code Snippet



  1. <Slider x:Name="VolumeBar" Width="60" Value="{Binding Path=Volume, ElementName=SmoothPlayer, Mode=TwoWay}" />

If you see, all we are doing here is saying that bind the Slider Value to the SmoothPlayer’s Volume  Path (or property).


For the Play / Pause and Stop buttons, we take a slightly different approach here:




Code Snippet



  1. <Button x:Name="PlayButton" Width="50" Click="PlayButton_Click" Loaded="PlayButton_Loaded"/>

  2. <Button x:Name="StopButton" Content="Stop" Width="50" Click="StopButton_Click" />

Here we want the following to happen:



  • For the Play/Pause button, we want to do some actions when the button is loaded and then a different action when it is clicked. For this, we hook these actions to PlayButton_Click and PlayButton_Loaded methods in the MainPage.xaml.cs file

  • For the Stop button we just need to handle the click so we hook up Click with StopButton_Click method.

In the MainPage.xaml.cs file we need to have these methods and do the needful there. Here is what we are doing in this example:


Note: There are other (better) ways of coding this but for simplicity I have tried to keep it very basic for this example


PlayButton_Loaded


 




Code Snippet



  1. private void PlayButton_Loaded(object sender, RoutedEventArgs e)

  2. {

  3.     //We need to prepopulate the value of Play/Pause button content, we need to check AutoPlay

  4.     switch (SmoothPlayer.AutoPlay)

  5.     {

  6.         case false:

  7.             PlayButton.Content = "Play";

  8.             break;

  9.         case true:

  10.             PlayButton.Content = "Pause";

  11.             break;

  12.     }

  13. }

 


PlayButton_Click




Code Snippet



  1. private void PlayButton_Click(object sender, RoutedEventArgs e)

  2. {

  3.     //Monitor the state of the content to determine the right action to take on this button being clicked

  4.     //and then change the text to reflect the next action

  5.     switch (SmoothPlayer.CurrentState)

  6.     {

  7.         case MediaElementState.Playing:

  8.             SmoothPlayer.Pause();

  9.             PlayButton.Content = "Play";

  10.             break;

  11.         case MediaElementState.Stopped:

  12.         case MediaElementState.Paused:

  13.             SmoothPlayer.Play();

  14.             PlayButton.Content = "Pause";

  15.             break;

  16.     }

  17. }

 


StopButton_Click




Code Snippet



  1. private void StopButton_Click(object sender, RoutedEventArgs e)

  2. {

  3.     //This should simply stop the playback

  4.     SmoothPlayer.Stop();

  5.     //We should also reflect the chang on the play button

  6.     PlayButton.Content = "Play";

  7. }

Summary


In this post, we looked at creating a basic player with common controls for playback of Smooth Streaming Content. Let me know if you have issues or need further help. In subsequent posts I would be diving into adding more features into this player.


The project sample for this example is availble here(SimplePlayer.zip).


Note: These are just samples meant for educational purposes and there is no guarantee conferred on the quality of code

No Comments