2013-08-28 67 views
0

我試圖一次使用MediaElement控件的一個片段播放.wmv。WPF:播放視頻文件的片段

我用一個定時器來播放/暫停視頻

但視頻播放一直不同步的。

任何想法如何解決這個問題?

public partial class MainWindow : Window 
{ 
    private System.Windows.Threading.DispatcherTimer VideoTimer; 
    private bool is_playing; 

    private void PlaySegment(long duration_miliseconds,long offset_milisecond=-1) 
    { 
     if (is_playing) return; 
     VideoTimer.Interval = new TimeSpan(10000*duration_miliseconds); 
     is_playing = true; 
     if (offset_milisecond>=0) 
      VideoControl.Position = new TimeSpan(10000*offset_milisecond); 
     VideoControl.Play(); 
     VideoTimer.Start(); 
    } 
    private void Timer_Stopped(object sender, EventArgs e) 
    { 
     VideoControl.Pause(); 
     is_playing = false; 
    } 
    public MainWindow() 
    { 
     InitializeComponent(); 
     VideoTimer = new System.Windows.Threading.DispatcherTimer(); 
     VideoTimer.Tick += new EventHandler(Timer_Stopped); 
    } 
} 

這是相關的XAML代碼: <MediaElement x:Name="VideoControl" LoadedBehavior="Manual" />

謝謝!

+0

你說,「視頻播放狀態越來越不同步」 ...是什麼讓不同步? – Sheridan

+0

與計時器不同步,例如計時器可能測量4300毫秒,但MediaElement會計時3500毫秒的視頻。 –

回答

1

打開擦洗...

VideoControl.ScrubbingEnabled = true;