我想在 WPF 中向后平滑播放视频。我正在使用 MediaElement
播放视频。我读过 this post,它建议定期更改 MediaElement.Position
以模拟倒带行为。
我尝试了以下代码来更改 MediaElement.Position
的位置
private void Button_Click(object sender, RoutedEventArgs e)
{
mePlayer.Pause(); //Pause the media player first
double m = 1 / frameRate; //Calculate the time for each frame
double t = 120; //Total length of video in seconds
mePlayer.Position = TimeSpan.FromMinutes(2); //Start video from 2 min
while (t >= 60) //Check if time exceeds 1 min
{
t = t - m; //Subtract the single frame time from total seconds
mePlayer.Position = TimeSpan.FromSeconds(t);//set position of video
}
}
在上面的代码中,我试图从 2 分钟到 1 分钟向后播放视频。
它在
mePlayer.Position = TimeSpan.FromSeconds(t)
上给了我“System.OverflowException”。如果有人知道如何在 WPF 中向后播放视频,请帮助我实现这种效果。谢谢你。
最佳答案
为了顺利完成,您应该使用 Timer
。假设帧速率为 24 fps,则意味着每 1/24 = 0.0416 秒或大约 42 毫秒一帧。因此,如果您的计时器每 42 毫秒计时一次,您可以将 mePlayer.Position
向后移动:
XAML:
<MediaElement x:Name="mePlayer" Source="C:\Sample.mp4"
LoadedBehavior="Manual" ScrubbingEnabled="True"/>
代码:
System.Windows.Threading.DispatcherTimer dispatcherTimer;
int t = 240000; // 4 minutes = 240,000 milliseconds
public MainWindow()
{
InitializeComponent();
dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
//tick every 42 millisecond = tick 24 times in one second
dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 42);
}
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
// Go back 1 frame every 42 milliseconds (or 24 fps)
t = t - 42;
mePlayer.Position = TimeSpan.FromMilliseconds(t);
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
mePlayer.Play();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
// Pause and go to 4th minute of the video then start playing backward
mePlayer.Pause();
mePlayer.Position = TimeSpan.FromMinutes(4);
dispatcherTimer.Start();
}
关于c# - 如何在 WPF 中向后播放视频?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30955604/