我希望在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中向后播放视频,请帮助我实现此效果。谢谢。
答案 0 :(得分:1)
为了顺利完成,您应该使用 function validItems(items) {
// create a valid items array. This will make maintaining valid item codes easier. and keep your code readable.
var valid = ["IT00","0144","6A1L","4243","O3D5","44SG","CE64","54FS","4422"];
var error = false;
for (i = 0; i < items.length; i++) {
// Type safe test. Always use 3 === isntead of == your test would have returned true on eveyrthing.
if (error === false) {
if(valid.indexOf(items[i]) === -1) {
// immedeately escape
return items[i];
} /*else {// Totally uneccesary
error = false;
}
} else {
// not needed here. this also escaped your loop after first iteration.
if (error !== false) {return error;} else {return "";}
}*/
}
// we return here because we know the loop is done then.
return error;
}
var items = ["IT00","0144","6A1L"];
alert(validItems(items));
。假设帧速率是24 fps,则意味着每1/24 = 0.0416秒或大约42毫秒是一帧。因此,如果您的计时器每42ms计时一次,您可以向后移动Timer
:
XAML:
mePlayer.Position
代码:
<MediaElement x:Name="mePlayer" Source="C:\Sample.mp4"
LoadedBehavior="Manual" ScrubbingEnabled="True"/>