Question

I have to play a video demonstration when the user doesn't touch the mouse for few seconds.

<Window x:Class="IHM.Animation"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  ResizeMode="NoResize" WindowState="Maximized" WindowStyle="None" WindowStartupLocation="CenterScreen"  >
    <Grid>
        <MediaElement HorizontalAlignment="Left"  Name="video" Height="221" Margin="160,255,0,0" VerticalAlignment="Top" Width="436" />
    </Grid>
</Window>

For the csharp class, I have this:

 public partial class Animation : Window
 {
    public Animation()
    {
        InitializeComponent();

        MediaPlayer player = new MediaPlayer();
        player.Open(new Uri(@"airplane.mpg", UriKind.Relative));
        VideoDrawing drawing = new VideoDrawing {Rect = new Rect(0, 0, System.Windows.SystemParameters.PrimaryScreenHeight, System.Windows.SystemParameters.PrimaryScreenWidth)};
        player.Play();
        DrawingBrush brush = new DrawingBrush(drawing);
        Background = brush;
        MouseMove += (sender, args) =>
        {
            player.Stop();
           Close();
        };
    player.MediaEnded += (sender, args) => Close();
    }
}

But, I have a black rectangle without son or image.the uri of the video is correct but it doesn't work.

Why is the video not working, how can I fix it?

Was it helpful?

Solution

I don't see you actually add the Visual to the UI. It's just created and run in the code-behind.

Updated Answer:

MediaPlayer player = new MediaPlayer();

public Window1() {
  InitializeComponent();
  player.Open(new Uri("airplane.mpg", UriKind.Relative));
  VideoDrawing drawing = new VideoDrawing {Rect = new Rect(0, 0, 800, 600), Player = player};
  player.Play();
  DrawingBrush brush = new DrawingBrush(drawing);
  Background = brush;
  player.MediaOpened += (sender, args) => MouseMove += OnMouseMove;
  player.MediaEnded += (sender, args) => {
    MouseMove -= OnMouseMove;
    Close();
  };
}

private void OnMouseMove(object sender, MouseEventArgs mouseEventArgs) {
  player.Stop();
  Close();
}

Update:

Sample Download Link

Another Update:

So the issue was also with having the MouseMove event firing too soon. Switching it to only capture MouseEvents after the MediaOpened event sorts that issue out.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top