Windows apps
Collapse the table of content
Expand the table of content
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

MediaState Enumeration

Specifies the states that can be applied to a MediaElement for the LoadedBehavior and UnloadedBehavior properties.

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

public enum MediaState
<object property="enumerationMemberName" .../>

Member nameDescription
ManualThe state used to control a MediaElement manually. Interactive methods like Play and Pause can be used. Media will preroll but not play when the MediaElement is assigned valid media source.
PlayThe state used to play the media. . Media will preroll automatically being playback when the MediaElement is assigned valid media source.
CloseThe state used to close the media. All media resources are released (including video memory).
PauseThe state used to pause the media. Media will preroll but remains paused when the MediaElement is assigned valid media source.
StopThe state used to stop the media. Media will preroll but not play when the MediaElement is assigned valid media source. Media resources are not released.

The following example shows how to control playback of media using a MediaElement. The example creates a simple media player that allows you to play, pause, stop, and skip back and forth in the media as well as adjust the volume and speed ratio.

The code below creates the UI.


The LoadedBehavior property of MediaElement must be set to Manual in order to be able to interactively stop, pause, and play the media.

<Page xmlns=""
  x:Class="SDKSample.MediaElementExample" >

  <StackPanel Background="Black">

    <!-- To interactively stop, pause, and play the media, the LoadedBehavior 
           property of the MediaElement must be set to "Manual". -->
    <MediaElement Source="media\numbers.wmv" Name="myMediaElement" 
     Width="450" Height="250" LoadedBehavior="Manual" UnloadedBehavior="Stop" Stretch="Fill" 
     MediaOpened="Element_MediaOpened" MediaEnded="Element_MediaEnded"/>

    <StackPanel HorizontalAlignment="Center" Width="450" Orientation="Horizontal">

      <!-- Play button. -->
      <Image Source="images\UI_play.gif" MouseDown="OnMouseDownPlayMedia" Margin="5" />

      <!-- Pause button. -->
      <Image Source="images\UI_pause.gif" MouseDown="OnMouseDownPauseMedia" Margin="5" />

      <!-- Stop button. -->
      <Image Source="images\UI_stop.gif" MouseDown="OnMouseDownStopMedia" Margin="5" />

      <!-- Volume slider. This slider allows a Volume range between 0 and 1. -->
      <TextBlock Foreground="White" VerticalAlignment="Center" Margin="5"  >Volume</TextBlock>
      <Slider Name="volumeSlider" VerticalAlignment="Center" ValueChanged="ChangeMediaVolume" 
       Minimum="0" Maximum="1" Value="0.5" Width="70"/>

      <!-- Volume slider. This slider allows a Volume range between 0 and 10. -->
      <TextBlock Foreground="White" Margin="5"  VerticalAlignment="Center">Speed</TextBlock>
      <Slider Name="speedRatioSlider" VerticalAlignment="Center" ValueChanged="ChangeMediaSpeedRatio" 
       Value="1" Width="70" />

      <!-- Seek to slider. Ths slider allows you to jump to different parts of the media playback. -->
      <TextBlock Foreground="White" Margin="5"  VerticalAlignment="Center">Seek To</TextBlock>
      <Slider Name="timelineSlider" Margin="5" ValueChanged="SeekToMediaPosition" Width="70"/>


The code below implements the functionality of the sample UI controls. The Play, Pause, and Stop methods are used to respectively play, pause and stop the media. Changing the Position property of the MediaElement allows you to skip around in the media. Finally, the Volume and SpeedRatio properties are used to adjust the volume and playback speed of the media.

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Input;

namespace SDKSample

   public partial class MediaElementExample : Page

      // Play the media. 
      void OnMouseDownPlayMedia(object sender, MouseButtonEventArgs args)

         // The Play method will begin the media if it is not currently active or  
         // resume media if it is paused. This has no effect if the media is 
         // already running.

         // Initialize the MediaElement property values.


      // Pause the media. 
      void OnMouseDownPauseMedia(object sender, MouseButtonEventArgs args)

         // The Pause method pauses the media if it is currently running. 
         // The Play method can be used to resume.


      // Stop the media. 
      void OnMouseDownStopMedia(object sender, MouseButtonEventArgs args)

         // The Stop method stops and resets the media to be played from 
         // the beginning.


      // Change the volume of the media. 
      private void ChangeMediaVolume(object sender, RoutedPropertyChangedEventArgs<double> args)
         myMediaElement.Volume = (double)volumeSlider.Value;

      // Change the speed of the media. 
      private void ChangeMediaSpeedRatio(object sender, RoutedPropertyChangedEventArgs<double> args)
         myMediaElement.SpeedRatio = (double)speedRatioSlider.Value;

      // When the media opens, initialize the "Seek To" slider maximum value
      // to the total number of miliseconds in the length of the media clip. 
      private void Element_MediaOpened(object sender, EventArgs e)
         timelineSlider.Maximum = myMediaElement.NaturalDuration.TimeSpan.TotalMilliseconds;

      // When the media playback is finished. Stop() the media to seek to media start. 
      private void Element_MediaEnded(object sender, EventArgs e)

      // Jump to different parts of the media (seek to).  
      private void SeekToMediaPosition(object sender, RoutedPropertyChangedEventArgs<double> args)
         int SliderValue = (int)timelineSlider.Value;

         // Overloaded constructor takes the arguments days, hours, minutes, seconds, miniseconds. 
         // Create a TimeSpan with miliseconds equal to the slider value.
         TimeSpan ts = new TimeSpan(0, 0, 0, 0, SliderValue);
         myMediaElement.Position = ts;

      void InitializePropertyValues()
         // Set the media's starting Volume and SpeedRatio to the current value of the 
         // their respective slider controls.
         myMediaElement.Volume = (double)volumeSlider.Value;
         myMediaElement.SpeedRatio = (double)speedRatioSlider.Value;


Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0

Community Additions

© 2017 Microsoft