Partager via


CurrentState

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets the status of the MediaElement object.

value = object.CurrentState

Property Value

Type: MediaState

The current state of the MediaElement object. The state can be one of the following (as defined in the MediaState enumeration): Buffering, Closed, Error, Opening, Paused, Playing, or Stopped.

This property is read-only. The default value is Closed.

Managed Equivalent

CurrentState

Remarks

When the value of this property changes, the MediaElement raises its CurrentStateChanged event.

CurrentState and CurrentStateChanged are most relevant if your application defines a user interface that permits transport control of the media (ultimately using MediaElement methods such as Play and Stop). Certain media states imply that calls to such methods will not be able to execute immediately, which you could choose to reflect in your user interface by temporarily providing visual feedback, or you could also provide fallback information based on state from within your event handlers. For example, if the current state is Buffering, a call to the Play method will not be immediately responsive until the state changes to another state such as Playing or Paused.

Changing the value of Source on a MediaElement will change the state to Opening, unless Source is set to null, in which case the state is Closed.

If the state is Error, that state change will also raise the MediaFailed event.

The meanings of the states and the possible application responses to them are influenced by the type of media that is being played. For example, live streaming media cannot be paused and will never enter the Paused state.

Your code should not rely on states occurring in an exact order. The CurrentStateChanged event reports that a change has occurred, and CurrentState reports the current state, but subframe transitory states might not raise CurrentStateChanged. CurrentStateChanged and CurrentState are provided so you can use the reported information to change the user interface, and reporting every subframe transitory state is not necessary for this scenario.

If the source is an ASX file, state is captured for the current media that is being processed from the playlist. States for referenced media in the playlist that are not the current media being processed are not captured. Downloading progress to the other media in the playlist occurs in the background, and the next item in the playlist will begin downloading 20 seconds before the end of the current entry.

Example

The following example demonstrates one way to display the CurrentState of a MediaElement. It creates a MediaElement and several buttons for controlling media playback. To display the current state of the MediaElement, the example registers for the CurrentStateChanged event and uses an event handler to update a TextBlock.

<Canvas 
  xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
  Width="300" Height="300">

  <MediaElement 
    x:Name="media" 
    Source="xbox.wmv" 
    CurrentStateChanged="media_state_changed" 
    Width="300" Height="300"/>

  <!-- Stops media playback.-->
  <Canvas MouseLeftButtonDown="media_stop"
    Canvas.Left="10" Canvas.Top="265">
    <Rectangle Stroke="Black"
      Height="30" Width="55" RadiusX="5" RadiusY="5">
      <Rectangle.Fill>
        <RadialGradientBrush GradientOrigin="0.75,0.25">
          <GradientStop Color="Orange" Offset="0.0" />
          <GradientStop Color="Red" Offset="1.0" />
        </RadialGradientBrush>
      </Rectangle.Fill>
    </Rectangle>
    <TextBlock Canvas.Left="5" Canvas.Top="5">stop</TextBlock>
  </Canvas>

  <!-- Pauses media playback. -->
  <Canvas MouseLeftButtonDown="media_pause"
    Canvas.Left="70" Canvas.Top="265">
    <Rectangle Stroke="Black"
      Height="30" Width="55" RadiusX="5" RadiusY="5">
      <Rectangle.Fill>
        <RadialGradientBrush GradientOrigin="0.75,0.25">
          <GradientStop Color="Yellow" Offset="0.0" />
          <GradientStop Color="Orange" Offset="1.0" />
        </RadialGradientBrush>
      </Rectangle.Fill>
    </Rectangle>
    <TextBlock Canvas.Left="5" Canvas.Top="5">pause</TextBlock>
  </Canvas>

  <!-- Begins media playback. -->
  <Canvas MouseLeftButtonDown="media_begin"
    Canvas.Left="130" Canvas.Top="265">
    <Rectangle Stroke="Black" RadiusX="5" RadiusY="5"
      Height="30" Width="55">
      <Rectangle.Fill>
        <RadialGradientBrush GradientOrigin="0.75,0.25">
          <GradientStop Color="LimeGreen" Offset="0.0" />
          <GradientStop Color="Green" Offset="1.0" />
        </RadialGradientBrush>
      </Rectangle.Fill>
    </Rectangle>
    <TextBlock Canvas.Left="5" Canvas.Top="5">play</TextBlock>
  </Canvas>

  <TextBlock
    Canvas.Left="190" Canvas.Top="265"
    FontSize="12">CurrentState:</TextBlock>

  <TextBlock 
    x:Name="mediaStateTextBlock"
    Canvas.Left="190" Canvas.Top="280"
    FontSize="12"></TextBlock>

</Canvas>
function media_stop(sender, args)
{
    sender.findName("media").stop();
}

function media_pause(sender, args)
{
    sender.findName("media").pause();
}

function media_begin(sender, args)
{
    sender.findName("media").play();
}

function media_state_changed(sender, args)
{
    var mediaStateTextBlock = 
    sender.findName("mediaStateTextBlock");
    var media = sender.findName("media");
    mediaStateTextBlock.Text = media.CurrentState;
}

Applies To

MediaElement

See Also

Reference