当用户没有触摸鼠标几秒钟时,我必须播放视频演示。
<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是正确的但它不起作用。
为什么视频无效,我该如何解决?
答案 0 :(得分: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
事件过早发生。在MouseEvents
事件对该问题进行排序后,将其切换为仅捕获MediaOpened
。