
The VideoBrush and MediaElement Relationship
The VideoBrush object's current video frame is determined by its MediaElement. The following illustration shows the relationship between a MediaElement and a VideoBrush.
MediaElement and VideoBrush
.png)
When a MediaElement loads media and that MediaElement is used by a VideoBrush as its SourceName, the video referenced as the MediaElement Source is downloaded and decoded only one time. You do not suffer significant performance penalties by using a MediaElement and VideoBrush together, even if you choose to have both the MediaElement and VideoBrush display the video.
By pausing or stopping the MediaElement, you pause or stop the VideoBrush. The following example plays, pauses, and stops a VideoBrush by manipulating its MediaElement source.
<Grid x:Name="LayoutRoot" Background="White">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<MediaElement
x:Name="butterflyMediaElement"
Source="Butterfly.wmv" IsMuted="True"
Opacity="0.0" IsHitTestVisible="False" />
<TextBlock Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3"
FontFamily="Verdana" FontSize="120"
FontWeight="Bold" TextWrapping="Wrap"
Text="Video">
<!-- Paint the text with video. -->
<TextBlock.Foreground>
<VideoBrush SourceName="butterflyMediaElement" Stretch="UniformToFill" />
</TextBlock.Foreground>
</TextBlock>
<!-- Play -->
<Button Click="PlayMedia" Grid.Row="1" Grid.Column="0"
Width="120" Height="20" Content="Play" />
<!-- Pause -->
<Button Click="PauseMedia" Grid.Row="1" Grid.Column="1"
Width="120" Height="20" Content="Pause" />
<!-- Stop -->
<Button Click="StopMedia" Grid.Row="1" Grid.Column="2"
Width="120" Height="20" Content="Stop" />
</Grid>
private void PlayMedia(object sender, RoutedEventArgs e)
{
butterflyMediaElement.Play();
}
private void StopMedia(object sender, RoutedEventArgs e)
{
butterflyMediaElement.Stop();
}
private void PauseMedia(object sender, RoutedEventArgs e)
{
butterflyMediaElement.Pause();
}
Private Sub PlayMedia(ByVal sender As Object, ByVal e As RoutedEventArgs)
butterflyMediaElement.Play()
End Sub
Private Sub StopMedia(ByVal sender As Object, ByVal e As RoutedEventArgs)
butterflyMediaElement.Stop()
End Sub
Private Sub PauseMedia(ByVal sender As Object, ByVal e As RoutedEventArgs)
butterflyMediaElement.Pause()
End Sub
Run this sample.
The previous illustration shows a single VideoBrush, but it is possible for multiple VideoBrush objects to share the same MediaElement.
Although playing, pausing, or stopping a MediaElement affects its VideoBrush, some MediaElement operations and settings have no effect on the VideoBrush. The following MediaElement methods and properties also manipulate a VideoBrush:
The following MediaElement methods and properties do not affect VideoBrush: