c# - 如何在 WPF 和 C# 中暂停视频并从另一个位置播放它

标签 c# wpf xaml mediaelement dispatchertimer

我试图在测试代码中的特定位置暂停视频,并尝试从完全不同的位置开始播放。它最终确实从另一个点开始,但当它再次暂停时,它会移回到它注册的第一个实例。我想知道我在这段代码中做错了什么。

CS文件中的代码是

namespace timer_thread_and_gui
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
        DispatcherTimer timer;
        DispatcherTimer timer1;


    public MainWindow()
    {
        InitializeComponent();

   }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        timer1 = new DispatcherTimer();
        timer1.Tick += new EventHandler(dispatcherTimer_Tick1);
        timer1.Interval = new TimeSpan(0, 0, 10);

                    timer = new DispatcherTimer();
                    timer.Tick += new EventHandler(dispatcherTimer_Tick);
                    timer.Interval = new TimeSpan(0, 0, 5);
                    video_panel.Play();




            timer1.Start();
            timer.Start();




    }

    private void dispatcherTimer_Tick(object sender, EventArgs e)
    {
        this.draw_an_elipse();

       draw_an_elipse()
    {
       ++a;
       txt.Text = a.ToString();



       if (a%5==0)
       {
           video_panel.Stop();
           video_panel.Position = new TimeSpan(0, 18, 30);

           video_panel.Play();
           timer1.Start();
       }
    }

   private void dispatcherTimer_Tick1(object sender, EventArgs e)
   {

    video_panel.Pause();
    timer1.Stop();
   }

}

XAML 代码是

<MediaElement Height="266" HorizontalAlignment="Left" Margin="12,33,0,0" Name="video_panel" 
                  VerticalAlignment="Top" Width="474" 
                  LoadedBehavior="Manual" UnloadedBehavior="Stop" 
                  Source="c:\users\ayymmoo\documents\visual studio 2010\Projects\Unfinished.avi" />

最佳答案

如果您想根据视频停止的位置跳转到另一个位置,您的 TimeSpan 应该是相对于该停止位置的,而不是绝对的:

如果你想在暂停后跳过 10 秒,应该像这样:

MediaElement.Position += TimeSpan.FromSeconds(10);

关于c# - 如何在 WPF 和 C# 中暂停视频并从另一个位置播放它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13551697/

相关文章:

c# - 为构建轮询 Microsoft.VisualStudio.Services.Client 时出现 NullReferenceException

wpf - 无法覆盖由 TargetType 在单个特定控件上设置的全局 WPF 样式

c# - 如何在我的 ViewModel 中监听来自另一个 ViewModel 的更改?

c# - 在 UWP 中为页面导航转换创建自定义 NavigationTransitionInfo

c# - DataGridRowTemplateColumn - 高效地重构和使用样式?

c# - ReactiveCommand.CreateAsync 任务。如何使用按钮取消任务?

c# - 在 C# 中获取局域网的计算机名称

c# - 我们什么时候使用 ANTLR

c# - 不同线程中的渲染控制

c# - MVVM 绑定(bind)在设计时不起作用