WPF从a点到b点的视频循环

时间:2015-06-13 11:40:02

标签: wpf

有没有办法将WPF中的视频从A点循环到B? A& B表示时间。 例如,我想循环播放视频&循环从视频的1分钟开始,到视频的2分钟结束。

1 个答案:

答案 0 :(得分:2)

XAML

<MediaElement x:Name="MyVideo"
            Loaded="MyVideo_Loaded"
            LoadedBehavior="Manual"
            Source="C:\SomeFolder\SomeVideo.mp4" />

代码隐藏

public partial class MainWindow : Window
{
    private DispatcherTimer dispatcherTimer;

    /// <summary>
    /// Initializes a new instance of the MainWindow class.
    /// </summary>
    public MainWindow()
    {
        InitializeComponent();

        //Set up the DispatcherTimer with a Tick interval of 1 minute
        dispatcherTimer = new DispatcherTimer();
        dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
        dispatcherTimer.Interval = new TimeSpan(0, 1, 0);
    }

    //Occurs when MediaElement is Loaded
    private void MyVideo_Loaded(object sender, RoutedEventArgs e)
    {
        //Set MediaElement Position to 1 minute and Play
        MyVideo.Position = TimeSpan.FromMinutes(1);
        MyVideo.Play();

        //Start the timer
        dispatcherTimer.Start();
    }

    //Occurs every Tick
    private void dispatcherTimer_Tick(object sender, EventArgs e)
    {
        //At the end of a Tick period, reset the MediaElement Position and Play again
        MyVideo.Position = TimeSpan.FromMinutes(1);
        MyVideo.Play();

        //Disable/reset the timer
        dispatcherTimer.IsEnabled = false;

        //Restart the timer
        dispatcherTimer.Start();
    }
}