Export (0) Print
Expand All

MediaElement.SpeedRatio Property

Gets or sets the speed ratio of the media.

Namespace:  System.Windows.Controls
Assembly:  PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation

'Declaration
Public Property SpeedRatio As Double
<object SpeedRatio="double" .../>

Property Value

Type: System.Double
The speed ratio of the media. The valid range is between 0 (zero) and infinity. Values less than 1 yield slower than normal playback, and values greater than 1 yield faster than normal playback. Negative values are treated as 0. The default value is 1.

ExceptionCondition
InvalidOperationException

The Clock property is not Nothing.

This property can be set only when the Clock property is Nothing. When the Clock property is non-Nothing, the timing engine controls media playback behavior.

Audio playback may not occur unless the value of SpeedRatio is 1.

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.

NoteNote

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="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  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 you to change the speed of the media playback. -->
      <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"/>

    </StackPanel>
  </StackPanel>
</Page>

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.

Imports System
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Documents
Imports System.Windows.Navigation
Imports System.Windows.Shapes
Imports System.Windows.Data
Imports System.Windows.Media
Imports System.Windows.Input

Namespace SDKSample

    Partial Class MediaElementExample
        Inherits Page

        ' Play the media. 
        Sub OnMouseDownPlayMedia(ByVal sender As Object, ByVal args As MouseButtonEventArgs)

            ' 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.
            myMediaElement.Play()

            ' Initialize the MediaElement property values.
            InitializePropertyValues()

        End Sub 'OnMouseDownPlayMedia


        ' Pause the media. 
        Sub OnMouseDownPauseMedia(ByVal sender As Object, ByVal args As MouseButtonEventArgs)

            ' The Pause method pauses the media if it is currently running. 
            ' The Play method can be used to resume.
            myMediaElement.Pause()

        End Sub 'OnMouseDownPauseMedia


        ' Stop the media. 
        Sub OnMouseDownStopMedia(ByVal sender As Object, ByVal args As MouseButtonEventArgs)

            ' The Stop method stops and resets the media to be played from 
            ' the beginning.
            myMediaElement.Stop()

        End Sub 'OnMouseDownStopMedia


        ' Change the volume of the media. 
        Private Sub ChangeMediaVolume(ByVal sender As Object, ByVal args As RoutedPropertyChangedEventArgs(Of Double))
            myMediaElement.Volume = System.Convert.ToDouble(volumeSlider.Value)

        End Sub 'ChangeMediaVolume

        ' Change the speed of the media. 
        Private Sub ChangeMediaSpeedRatio(ByVal sender As Object, ByVal args As RoutedPropertyChangedEventArgs(Of Double))
            myMediaElement.SpeedRatio = System.Convert.ToDouble(speedRatioSlider.Value)

        End Sub 'ChangeMediaSpeedRatio

        ' 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 Sub Element_MediaOpened(ByVal sender As Object, ByVal args As RoutedEventArgs)
            timelineSlider.Maximum = myMediaElement.NaturalDuration.TimeSpan.TotalMilliseconds
        End Sub 

        ' When the media playback is finished. Stop() the media to seek to media start. 
        Private Sub Element_MediaEnded(ByVal sender As Object, ByVal args As RoutedEventArgs)
            myMediaElement.Stop()
        End Sub 

        ' Jump to different parts of the media (seek to).  
        Private Sub SeekToMediaPosition(ByVal sender As Object, ByVal args As RoutedPropertyChangedEventArgs(Of Double))
            Dim SliderValue As Integer = CType(timelineSlider.Value, Integer)

            ' Overloaded constructor takes the arguments days, hours, minutes, seconds, miniseconds. 
            ' Create a TimeSpan with miliseconds equal to the slider value. 
            Dim ts As New TimeSpan(0, 0, 0, 0, SliderValue)
            myMediaElement.Position = ts
        End Sub 

        ' Set the media's starting Volume and SpeedRatio to the current value of the 
        ' their respective slider controls. 
        Private Sub InitializePropertyValues()
            myMediaElement.Volume = System.Convert.ToDouble(volumeSlider.Value)
            myMediaElement.SpeedRatio = System.Convert.ToDouble(speedRatioSlider.Value)
        End Sub 
    End Class 'MediaElementExample
End Namespace 'SDKSample

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Show:
© 2014 Microsoft