CurrentState Property

Gets the status of the MediaElement.

XAML
Cannot be used in XAML.
Scripting
value = object.CurrentState

Property Value

MediaState

The current state of the MediaElement. 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.

Remarks

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

CurrentState and CurrentStateChanged are most relevant if your application defines UI that permits transport control of the media (ultimately using MediaElement methods such as Play, Stop, etc.) Certain media states imply that calls to such APIs will not be able to execute immediately, which you could choose to reflect in your UI 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 Play 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 MediaFailed.

The meanings of the states and the possible application responses to them are influenced by the type of media being played. For instance, live streaming media cannot be paused and will never enter that state. For details, see Supported Media Formats and Protocols.

Your code should not rely on states falling in an exact order. The CurrentStateChanged reports that a change has occured, and CurrentState reports the current state, but subframe transitory states might not raise CurrentStateChanged. The intention of the CurrentStateChanged / CurrentState API is that you can use the reported information for changes to UI, and reporting every subframe transitory state is not necessary for this scenario.

If the source is an ASX file, state is captured for the media that is the current media 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 will begin downloading the next item in the playlist 20 seconds before the end of the current entry.

Examples

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.

XAML
<Canvas 
  xmlns="http://schemas.microsoft.com/client/2007"
  xmlns:x="http://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>
JavaScript
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

Media Overview
Supported Media Formats and Protocols
MediaElement States
CurrentStateChanged
MediaElement

Community Additions

Show: