Share via


MediaElement.CurrentState Property

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

Gets the status of the MediaElement.

Namespace:  System.Windows.Controls
Assembly:  System.Windows (in System.Windows.dll)

Syntax

'Declaration
Public ReadOnly Property CurrentState As MediaElementState
public MediaElementState CurrentState { get; }

Property Value

Type: System.Windows.Media.MediaElementState
The current state of the MediaElement. The state can be one of the following (as defined in the MediaElementState enumeration): Buffering, Closed, Opening, Paused, Playing, or Stopped.
The default value is Closed.

Remarks

Dependency property identifier field: CurrentStateProperty

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.

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.

Your code should not rely on states occurring in an exact order. The CurrentStateChanged reports that a change has occurred, 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.

Run this sample

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>

    <MediaElement CurrentStateChanged="Media_State_Changed"
        x:Name="media" Source="xbox.wmv" Width="300" Height="300" 
                  Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="3" />

    <!-- Stops media playback.-->
    <Button Click="StopMedia" 
     Grid.Column="0" Grid.Row="1" Content="Stop" />

    <!-- Pauses media playback. -->
    <Button Click="PauseMedia" 
     Grid.Column="1" Grid.Row="1" Content="Pause" />

    <!-- Begins media playback. -->
    <Button Click="PlayMedia" 
     Grid.Column="2" Grid.Row="1" Content="Play" />

    <TextBlock
Grid.Column="0" Grid.Row="2" Margin="10" 
FontSize="12">CurrentState:</TextBlock>

    <TextBlock 
x:Name="mediaStateTextBlock"
Grid.Column="1" Grid.Row="2" Margin="0,10,0,0"
FontSize="12"></TextBlock>

</Grid>
Private Sub StopMedia(ByVal sender As Object, ByVal e As RoutedEventArgs)
    media.Stop()
End Sub

Private Sub PauseMedia(ByVal sender As Object, ByVal e As RoutedEventArgs)
    media.Pause()
End Sub

Private Sub PlayMedia(ByVal sender As Object, ByVal e As RoutedEventArgs)
    media.Play()
End Sub
Private Sub Media_State_Changed(ByVal sender As Object, ByVal e As EventArgs)
    mediaStateTextBlock.Text = media.CurrentState.ToString
End Sub
private void StopMedia(object sender, RoutedEventArgs e)
{
    media.Stop();
}
private void PauseMedia(object sender, RoutedEventArgs e)
{
    media.Pause();
}
private void PlayMedia(object sender, RoutedEventArgs e)
{
    media.Play();
}
private void Media_State_Changed(object sender, EventArgs e)
{
    mediaStateTextBlock.Text = media.CurrentState.ToString();
}

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.