我正在学习如何使用WPF mediaelement控制视频的教程。对于大多数部件,除了倒带视频功能外,一切都很好。我试图寻找解决方案并遇到一个线程,建议暂停视频并改变视频播放的位置。 所以我尝试了这个:
//timer updates the position of the slider
if(flagRewind == true)
{
try
{
if (playVideoMediaElement.NaturalDuration.HasTimeSpan)
{
//step the video back every second
playVideoMediaElement.Position -= TimeSpan.FromMilliseconds(1000);
videoSlider.Value = playVideoMediaElement.Position.TotalSeconds;
//we have re-winded to the zero position of the slider
if (videoSlider.Value <= 0)
{
//the video is done playing
playVideoMediaElement.Stop();
//get tag of button click, to change button icon appropriately (pause[||]/play [>])
tag = playPauseVideoButton.Tag.ToString();
//call method that changes the button image icon to either [||] or [>]
ChangePlayPauseButtonImageIcon();
IsPlaying(false);
playPauseVideoButton.IsEnabled = true;
//we are done re-winding
flagRewind = false;
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
}
}
这有效,但它正在跳帧,我尝试使用这样的按钮点击事件加速:
private void slowPlayVideoButton_Click(object sender, RoutedEventArgs e)
{
playVideoMediaElement.SpeedRatio = (double)-1;
}
但这只是停止(冻结)视频。如何使用速度比率平滑地回放视频? #Thanks!
答案 0 :(得分:0)
我所做的只是通过增加TimeSpan值来更改计时器间隔,我还更改了媒体元素的TimeSpan。代码现在看起来:
if(flagRewind == true)
{
//change the timer interval
timer.Interval = TimeSpan.FromMilliseconds(50.0);
try
{
if (playVideoMediaElement.NaturalDuration.HasTimeSpan)
{
//step the video back every second
playVideoMediaElement.Position -= TimeSpan.FromMilliseconds(50.0);
videoSlider.Value = playVideoMediaElement.Position.TotalSeconds;
if (videoSlider.Value <= 0)
{
//the video is done playing
playVideoMediaElement.Stop();
tag = playPauseVideoButton.Tag.ToString();
ChangePlayPauseButtonImageIcon();
IsPlaying(false);
playPauseVideoButton.IsEnabled = true;
flagRewind = false;
}
}
这对我来说非常好,当用户按下前进||时播放按钮我只是默认定时器间隔和媒体元素位置值。