Created
January 24, 2020 12:20
-
-
Save ArXen42/b46c7371da6b27e3caf9e388a1770e49 to your computer and use it in GitHub Desktop.
VideoBox.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// <inheritdoc /> | |
| /// <summary> | |
| /// Wrapper control around VlcControl. Provides basic UI for video playback. | |
| /// </summary> | |
| public partial class VideoBox : UserControl | |
| { | |
| private static readonly String[] MediaOptions = {":avcodec-hw=dxva2"}; //Should enable GPU acceleration | |
| private readonly Dispatcher _dispatcher; | |
| private Boolean _isChangingPosition; //Prevents (sometimes) unwanted feedback | |
| private Video3D _video; | |
| public VideoBox() | |
| { | |
| InitializeComponent(); | |
| _dispatcher = Dispatcher.CurrentDispatcher; | |
| Enabled = false; | |
| } | |
| public event EventHandler EncounteredPlaybackError; | |
| /// <summary> | |
| /// Video data used for playback. | |
| /// </summary> | |
| public Video3D Video | |
| { | |
| get => _video; | |
| set | |
| { | |
| if (_video != null) | |
| { | |
| VlcControl.Stop(); | |
| _video.Dispose(); | |
| } | |
| _video = value; | |
| if (_video != null) | |
| VlcControl.Play(_video.VideoStream, MediaOptions); | |
| Enabled = _video != null; | |
| } | |
| } | |
| /// <summary> | |
| /// Stops video playback. Video remains loaded into control but does not consume resources. | |
| /// </summary> | |
| public void Stop() | |
| { | |
| if (VlcControl.GetCurrentMedia() != null) | |
| VlcControl.Stop(); | |
| } | |
| /// <inheritdoc /> | |
| protected override void Dispose(Boolean disposing) | |
| { | |
| if (disposing) | |
| { | |
| Video = null; | |
| VlcControl.Dispose(); | |
| components?.Dispose(); | |
| } | |
| base.Dispose(disposing); | |
| } | |
| private void PlaybackTrackBar_ValueChanged(Object sender, EventArgs e) | |
| { | |
| if (!VlcControl.IsPlaying) | |
| return; | |
| if (_isChangingPosition) | |
| return; | |
| _isChangingPosition = true; | |
| VlcControl.Position = (Single) (PlaybackTrackBar.Value - PlaybackTrackBar.Minimum) / (PlaybackTrackBar.Maximum - PlaybackTrackBar.Minimum); | |
| _isChangingPosition = false; | |
| } | |
| private void PlayPauseButton_Click(Object sender, EventArgs e) | |
| { | |
| if (VlcControl.IsPlaying) | |
| VlcControl.Pause(); | |
| else if (VlcControl.State == MediaStates.Paused) | |
| VlcControl.Play(); | |
| else | |
| VlcControl.Play(_video.VideoStream, MediaOptions); | |
| } | |
| private void StopButton_Click(Object sender, EventArgs e) | |
| { | |
| Stop(); | |
| } | |
| private void VlcControl_EncounteredError(Object sender, VlcMediaPlayerEncounteredErrorEventArgs e) | |
| { | |
| _dispatcher.InvokeAsync(() => | |
| { | |
| Video = null; | |
| EncounteredPlaybackError?.Invoke(this, EventArgs.Empty); | |
| }); | |
| } | |
| private void VlcControl_Paused(Object sender, VlcMediaPlayerPausedEventArgs e) | |
| { | |
| _dispatcher.InvokeAsync(() => PlayPauseButton.Text = @"▶️"); | |
| } | |
| private void VlcControl_Playing(Object sender, VlcMediaPlayerPlayingEventArgs e) | |
| { | |
| _dispatcher.InvokeAsync(() => PlayPauseButton.Text = @"❚❚"); | |
| } | |
| private void VlcControl_PositionChanged(Object sender, VlcMediaPlayerPositionChangedEventArgs e) | |
| { | |
| if (_isChangingPosition) | |
| return; | |
| _dispatcher.InvokeAsync(() => | |
| { | |
| _isChangingPosition = true; | |
| PlaybackTrackBar.Value = | |
| (Int32) Math.Round(PlaybackTrackBar.Minimum + e.NewPosition * (PlaybackTrackBar.Maximum - PlaybackTrackBar.Minimum)); | |
| _isChangingPosition = false; | |
| }); | |
| } | |
| private void VlcControl_Stopped(Object sender, VlcMediaPlayerStoppedEventArgs e) | |
| { | |
| _dispatcher.InvokeAsync(() => | |
| { | |
| _video.VideoStream.Position = 0; | |
| PlaybackTrackBar.Value = PlaybackTrackBar.Minimum; | |
| PlayPauseButton.Text = @"▶️"; | |
| }); | |
| } | |
| private void VlcControl_VlcLibDirectoryNeeded(Object sender, VlcLibDirectoryNeededEventArgs e) | |
| { | |
| e.VlcLibDirectory = new DirectoryInfo(Path.Combine(Environment.CurrentDirectory, @"VLC\")); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment