2

当用户几秒钟没有触摸鼠标时,我必须播放视频演示。

<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>

对于 csharp 类,我有这个:

 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();
    }
}

但是,我有一个没有儿子或图像的黑色矩形。视频的 uri 是正确的,但它不起作用。

为什么视频无法播放,我该如何解决?

4

1 回答 1

3

我没有看到您实际上将 Visual 添加到 UI。它只是在代码隐藏中创建并运行。

更新答案:

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();
}

更新:

样本下载链接

另一个更新:

所以问题还在于MouseMove事件触发太快。将其切换为仅MouseEventsMediaOpened事件解决该问题后捕获。

于 2013-05-13T14:17:49.920 回答