Events

The Microsoft.Web.Media.SmoothStreaming namespace defines events that occur when Smooth Streaming manifest files load and the media content plays. You can assign delegate methods to handle events, extend application features, and manage exceptions. The event handlers demonstrated in the previous topic, Silverlight Applications, were automatically implemented by the Silverlight application framework. You have to write a little more code to define application logic that handles events of the SmoothStreamingMediaElement.

This topic demonstrates the use of three events:

The examples in this topic extend the sample created in the topic Silverlight Applications. Other event scenarios are demonstrated in the topics linked at the bottom of the page.

Media Ended Event

The MediaEnded event occurs when a media stream that has been playing ends. To catch and respond to the MediaEnded event, define a delegate as shown in the following code segment and add it to the MainPage() constructor of the sample in the topic Silverlight Applications. The += assignment creates a new RoutedEventHandler and names it SmoothPlayer_MediaEnded.

SmoothPlayer.MediaEnded += new RoutedEventHandler(SmoothPlayer_MediaEnded);

The following code shows the SmoothPlayer_MediaEnded delegate method that handles the MediaEnded event. In this case, when the event occurs, the handler simply displays text in an output block beneath the player display. The current state of the SmoothStreamingMediaElement is obtained and appended to the text by casting the sender parameter to SmoothStreamingMediaElement and by reading the Microsoft.Web.Media.SmoothStreaming.SmoothStreamingMediaElement.CurrentState property. The caption of Play is reset.

  void SmoothPlayer_MediaEnded(object sender, RoutedEventArgs e)
  {
      OutputText.Text = "Media Ended. SmoothPlayer CurrentState: " +
                      ((SmoothStreamingMediaElement)sender).CurrentState;
      PlayButton.Content = "Play";
  }

Position Property and Seek Completed Event

You can assign the Position property of the SmoothStreamingMediaElement to a new position in the stream and the player will seek to the new position. Simply assign a TimeSpan object to the Position property. The TimeSpan object specifies the time interval from the beginning of the stream.

To implement a button that will seek to a new position, add the following line to MainPage.xaml of the sample application implemented in the topic Silverlight Applications. Add the SeekButton specification after the StopButton and before the ComboBox.

  <Button x:Name="SeekButton" Width="50" Content="+5 Secs" Click="SeekButton_Click" />

Add a variable to the MainPage class to specify the time interval that will be added to the position on each seek operation.

  TimeSpan spanAdd = new TimeSpan(0,0,5); // Five second inteval.

Each time the user clicks SeekButton, the following event handler adds a five-second interval to the position property until the current position plus 5 seconds is greater than the length of the media stream as specified by the EndPosition property.

  private void SeekButton_Click(object sender, RoutedEventArgs e)
  {
      if ((SmoothPlayer.Position + spanAdd) < SmoothPlayer.EndPosition)
      {
          SmoothPlayer.Position += spanAdd;
      }
  }

Seek Completed Event

The SeekCompleted event occurs when the Position set operation started by the previous code completes. Create a delegate to catch this event by adding the following code to the MainPage() constructor.

  SmoothPlayer.SeekCompleted += 
       new EventHandler<SeekCompletedEventArgs>(SmoothPlayer_SeekCompleted);

This is a generic event handler that specifies the type SeekCompletedEventArgs. The handler method is named SmoothPlayer_SeekCompleted. The method simply displays the new position to an output text block.

  void SmoothPlayer_SeekCompleted(object sender, SeekCompletedEventArgs e)
  {
      OutputText.Text = "  Seek complete at: " +
                 ((SmoothStreamingMediaElement)sender).Position;
  }

Note

The topic Timeline Markers and Events shows how to define Microsoft.Web.Media.SmoothStreaming.TimelineEvent markers in media streams and how to handle the events they generate.

You can extend event functionality in many ways. For longer media streams, it may be preferable to use a slider bar instead of the seek button shown in the previous example. For an implementation of the slider bar, see Position in Stream.

Exception Event

The SmoothStreamingErrorOccurred exception event can be handled as demonstrated for other events in this topic.

Assign a delegate to handle the event.

SmoothPlayer.SmoothStreamingErrorOccurred += 
  new EventHandler<SmoothStreamingErrorEventArgs>(SmoothPlayer_SmoothStreamingErrorOccurred);

Implement the delegate.

  void SmoothPlayer_SmoothStreamingErrorOccurred(object sender, 
                                  SmoothStreamingErrorEventArgs e)
  {
        OutputText.Text = "Error: " + e.ErrorCode + "; " + e.ErrorMessage;
  }

This handler displays the error code and message to a text block. A full application should implement error-handling logic so that processing does not stop responding if an exception occurs.

See Also

Concepts

Silverlight Applications

Timeline Markers and Events

Scheduling Media Clips

Select and Monitor Bitrate