WPF Play Video в обратном направлении
Я создал видеоплеер с помощью Media Element в WPF, где я могу сделать паузу, воспроизведение, остановку и перемотку назад в какую-то конкретную позицию.
Но я бы хотел воспроизвести видео в обратном направлении (в противоположном направлении), когда нажата кнопка "Воспроизвести назад", и воспроизвести в прямом направлении, если нажать "вперед".
Есть ли способ достичь этого,
Вот мой код,
<ToolBar Margin="5">
<Button x:Name="Open" Click="ControlVideoFlow" Margin="2" Height="30" Width="30" Cursor="Hand">
<Image Source="..\..\Images\folder.png" />
</Button>
<Separator />
<Button x:Name="PlayPause" Click="ControlVideoFlow" Margin="2" Height="30" Width="30" Cursor="Hand">
<Image x:Name="imgPlayPause" Source="..\..\Images\play.png" />
</Button>
<Separator />
<Button x:Name="Stop" Click="ControlVideoFlow" Margin="2" Height="30" Width="30" Cursor="Hand">
<Image Source="..\..\Images\stop.png" />
</Button>
<Separator />
<Button x:Name="PrevFrame" Click="ControlVideoFlow" Margin="2" Height="30" Width="30" Cursor="Hand">
<Image Source="..\..\Images\slow_frwd.png" />
</Button>
<Separator />
<Button x:Name="NextFrame" Click="ControlVideoFlow" Margin="2" Height="30" Width="30" Cursor="Hand">
<Image Source="..\..\Images\fast_frwd.png" />
</Button>
<Separator />
<Button x:Name="Refresh" Click="ControlVideoFlow" Margin="2" Height="30" Width="30" Cursor="Hand">
<Image Source="..\..\Images\refresh.png" />
</Button>
</ToolBar>
<MediaElement Name="mePlayer" LoadedBehavior="Manual" ScrubbingEnabled="True" Stretch="UniformToFill" MediaFailed="mePlayer_MediaFailed" UnloadedBehavior="Manual" />
<StatusBar Background="#E0DFE1" VerticalAlignment="Top" Margin="5">
<StatusBar.ItemsPanel>
<ItemsPanelTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
</Grid>
</ItemsPanelTemplate>
</StatusBar.ItemsPanel>
<StatusBarItem>
<TextBlock Name="lblProgressStatus">00:00:00</TextBlock>
</StatusBarItem>
<StatusBarItem Grid.Column="1" HorizontalContentAlignment="Stretch" Cursor="Hand">
<Slider Name="sliProgress" IsMoveToPointEnabled="True" PreviewMouseLeftButtonUp="sliProgress_PreviewMouseLeftButtonUp" Thumb.DragStarted="sliProgress_DragStarted" Thumb.DragCompleted="sliProgress_DragCompleted" ValueChanged="sliProgress_ValueChanged" />
</StatusBarItem>
<StatusBarItem Grid.Column="2">
<ProgressBar Name="pbVolume" Width="50" Height="12" Maximum="1" Value="{Binding ElementName=mePlayer, Path=Volume}" />
</StatusBarItem>
</StatusBar>
Код позади,
public bool mediaPlayerIsPlaying = false;
private bool userIsDraggingSlider = false;
public string control = string.Empty;
DispatcherTimer timer = new DispatcherTimer();
public VideoAnalyser()
{
InitializeComponent();
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += timer_Tick;
timer.Start();
}
private void timer_Tick(object sender, EventArgs e)
{
if ((mePlayer.Source != null) && (mePlayer.NaturalDuration.HasTimeSpan) && (!userIsDraggingSlider))
{
Dispatcher.Invoke(() =>
{
sliProgress.Minimum = 0;
sliProgress.Maximum = mePlayer.NaturalDuration.TimeSpan.TotalSeconds;
sliProgress.Value = mePlayer.Position.TotalSeconds;
});
}
}
private void sliProgress_PreviewMouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
mePlayer.Position = TimeSpan.FromSeconds(sliProgress.Value);
}
private void sliProgress_DragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e)
{
userIsDraggingSlider = true;
}
private void sliProgress_DragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
{
userIsDraggingSlider = false;
mePlayer.Position = TimeSpan.FromSeconds(sliProgress.Value);
}
private void sliProgress_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
lblProgressStatus.Text = TimeSpan.FromSeconds(sliProgress.Value).ToString(@"hh\:mm\:ss");
}
private void mePlayer_MediaFailed(object sender, ExceptionRoutedEventArgs e)
{
MessageBox.Show("Media Failed");
}
private async void ControlVideoFlow(object sender, RoutedEventArgs e)
{
try
{
Button button = (Button)sender;
switch (button.Name)
{
case "Open":
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Media files (*.mp3;*.mpg;*.mpeg;*.mp4)|*.mp3;*.mpg;*.mpeg;*.mp4|All files (*.*)|*.*";
openFileDialog.Multiselect = true;
if (openFileDialog.ShowDialog() == true)
{
mePlayer.Source = new Uri(openFileDialog.FileName, UriKind.Relative);
mePlayer.Play();
mePlayer.Pause();
mediaPlayerIsPlaying = false;
imgPlayPause.Source = new BitmapImage(new Uri("pack://application:,,,/Images/play.png"));
control = "pause";
cnvMplayer.Visibility = Visibility.Visible;
await Task.Delay(300);
grdMplayer.Visibility = Visibility.Collapsed;
}
break;
case "PlayPause":
if ((mePlayer != null) && (mePlayer.Source != null))
{
switch (control)
{
case "":
imgPlayPause.Source = new BitmapImage(new Uri("pack://application:,,,/Images/play.png"));
mePlayer.Pause(); control = "pause";
mediaPlayerIsPlaying = false;
break;
case "pause":
imgPlayPause.Source = new BitmapImage(new Uri("pack://application:,,,/Images/pause.png"));
mePlayer.Play(); control = "";
mediaPlayerIsPlaying = true;
break;
};
}
break;
case "Stop":
if ((mePlayer != null) && (mePlayer.Source != null))
{
mePlayer.Stop();
mediaPlayerIsPlaying = false;
imgPlayPause.Source = new BitmapImage(new Uri("pack://application:,,,/Sources/Images/play.png"));
control = "pause";
sliProgress.Value = 0;
grdMplayer.Visibility = Visibility.Visible;
cnvMplayer.Visibility = Visibility.Collapsed;
mePlayer.Source = null;
}
break;
case "PrevFrame":
frameReduceorIncrease(-30);
break;
case "NextFrame":
frameReduceorIncrease(30);
break;
case "Refresh":
TimeSpan position = mePlayer.Position;
Uri source = mePlayer.Source;
mePlayer.Source = null;
mePlayer.Source = source;
imgPlayPause.Source = new BitmapImage(new Uri("pack://application:,,,/Images/play.png"));
mePlayer.Play(); control = "pause";
mediaPlayerIsPlaying = false;
mePlayer.Position = position - TimeSpan.FromMilliseconds(10);
await Task.Delay(TimeSpan.FromMilliseconds(500));
mePlayer.Pause();
break;
};
}
catch (Exception ex)
{
}
}