2013-05-13 65 views
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一個黑色矩形是正確的,但它不工作。

爲什麼視頻無法正常工作,我該如何解決它?

+0

你可以在Windows Media Player中播放視頻嗎?它工作嗎? – 2013-05-13 14:30:36

+0

看到我的更新代碼 – 2013-05-13 14:38:16

+0

首先,註釋掉MouseMove處理程序,看看它是否工作。 – Dusan 2013-05-13 15:19:30

回答

3

我沒有看到你實際上將視覺添加到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(); 
} 

更新:

Sample Download Link

另一個更新:

所以這個問題也與具有MouseMove事件觸發太快。在MediaOpened事件對此問題進行排序後,將其切換爲僅捕獲MouseEvents

+0

請看我的更新。該視頻沒有打開! – 2013-05-13 14:37:05

+1

@Lamloumi我已經編輯了我的答案,並附有示例下載鏈接,您可以下載並觀看。這對我來說可以。如果這個示例對你來說工作正常,那麼這個問題不是與代碼有關,而是你試圖播放的實際文件。這可能是整個負載問題,以開始驗證URL是否有效和分類。另外請記住,在我發佈的代碼中,我們幾乎渲染了媒體'DrawingBrush'的窗口背景,因此,如果採用這種方法,則不需要在xaml中具有'MediaElement'。 – Viv 2013-05-13 14:44:09

+0

感謝問題是,當我debbug程序,處理程序'player.MediaEnded'並沒有等待視頻的結尾 – 2013-05-13 14:51:10