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