views:

1212

answers:

2

Is there a way using WPF to get a video element to start playing when a user puts there mouse pointer over the element? I am wanting to make an interactive digital resource and want a clip of the movie to play when the mouse over the element to click to take them to the movie section. Any help would be great.

This is going to all be contained in a windows application.

+1  A: 

If you're using a MediaElement to play your video, just listen for the MouseEntered Event and call Play().

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

Here's an example:

In C# code-behind:

public partial class Window1 : Window
{
 public Window1()
 {
  InitializeComponent();
 }

 private void mediaElement1_MouseEnter(object sender, MouseEventArgs e)
 {
  mediaElement1.Play();
 }

 private void mediaElement1_MouseLeave(object sender, MouseEventArgs e)
 {
  mediaElement1.Stop();
 }

 private void mediaElement1_Loaded(object sender, RoutedEventArgs e)
 {
  mediaElement1.Pause();
 }
}

In XAML:

<Window x:Class="VideoTest.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <MediaElement Margin="12" 
                      Name="mediaElement1" 
                      Source="mediaFile.avi"
                      LoadedBehavior="Manual"                      
                      MouseEnter="mediaElement1_MouseEnter"
                      MouseLeave="mediaElement1_MouseLeave"
                      Loaded="mediaElement1_Loaded"
                      />
    </Grid>
</Window>
Eclipse
A: 

Found an issue with the

<MediaElement Margin="12" 
                  Name="mediaElement1" 
                  Source="mediaFile.avi"
                  LoadedBehavior="Manual"                      
                  MouseEnter="mediaElement1_MouseEnter"
                  MouseLeave="mediaElement1_MouseLeave"
                  Loaded="mediaElement1_Loaded"
                  />

It doesnt seem to like the

Loaded="mediaElement1_Loaded"

So I changed my Initialize in my code behind to contain the

mediaElement1.Pause();

And it is working just fine.

Ironsides