Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.IO;
- #region Vlc
- using Microsoft.Win32;
- using Vlc.DotNet.Core;
- using Vlc.DotNet.Core.Interops.Signatures.LibVlc.Media;
- using Vlc.DotNet.Core.Interops.Signatures.LibVlc.MediaListPlayer;
- using Vlc.DotNet.Core.Medias;
- using Vlc.DotNet.Wpf;
- using System.ComponentModel;
- #endregion
- namespace VLC_wpf
- {
- /// <summary>
- /// Interaction logic for MainWindow.xaml
- /// </summary>
- public partial class MainWindow : Window
- {
- private bool positionChanging;
- public MainWindow()
- {
- initVLCPlayer();
- InitializeComponent();
- sliderPosition.ApplyTemplate();
- System.Windows.Controls.Primitives.Thumb thumb = (sliderPosition.Template.FindName(
- "PART_Track", sliderPosition) as System.Windows.Controls.Primitives.Track).Thumb;
- thumb.MouseEnter
- += new MouseEventHandler(thumb_MouseEnter);
- Closing += MainWindowOnClosing;
- }
- private void thumb_MouseEnter(object sender, MouseEventArgs e)
- {
- if (e.LeftButton == MouseButtonState.Pressed
- && e.MouseDevice.Captured == null)
- {
- MouseButtonEventArgs args = new MouseButtonEventArgs(
- e.MouseDevice, e.Timestamp, MouseButton.Left);
- args.RoutedEvent = MouseLeftButtonDownEvent;
- (sender as System.Windows.Controls.Primitives.Thumb).RaiseEvent(args);
- }
- }
- private void initVLCPlayer()
- {
- /*if (Directory.Exists("C:\\Program Files\\VideoLAN\\VLC"))
- {
- // Set libvlc.dll and libvlccore.dll directory path
- VlcContext.LibVlcDllsPath = @"C:\Program Files\VideoLAN\VLC";
- // Set the vlc plugins directory path
- VlcContext.LibVlcPluginsPath = @"C:\Program Files\VideoLAN\VLC\plugins";
- }
- else
- {
- // Set libvlc.dll and libvlccore.dll directory path
- VlcContext.LibVlcDllsPath = @"C:\Program Files (x86)\VideoLAN\VLC";
- // Set the vlc plugins directory path
- VlcContext.LibVlcPluginsPath = @"C:\Program Files (x86)\VideoLAN\VLC\plugins";
- }*/
- if (Environment.Is64BitOperatingSystem)
- {
- VlcContext.LibVlcDllsPath = CommonStrings.LIBVLC_DLLS_PATH_DEFAULT_VALUE_AMD64;
- VlcContext.LibVlcPluginsPath = CommonStrings.PLUGINS_PATH_DEFAULT_VALUE_AMD64;
- /*// Set libvlc.dll and libvlccore.dll directory path
- VlcContext.LibVlcDllsPath = @".\x86"; //@"C:\Program Files\VideoLAN\vlc-2.0.5";
- // Set the vlc plugins directory path
- VlcContext.LibVlcPluginsPath = @".\plugins"; //@"C:\Program Files\VideoLAN\vlc-2.0.5\plugins";*/
- }
- else
- {
- VlcContext.LibVlcDllsPath = CommonStrings.LIBVLC_DLLS_PATH_DEFAULT_VALUE_X86;
- VlcContext.LibVlcPluginsPath = CommonStrings.PLUGINS_PATH_DEFAULT_VALUE_X86;
- /*// Set libvlc.dll and libvlccore.dll directory path
- VlcContext.LibVlcDllsPath = @".\x64"; //@"C:\Program Files\VideoLAN\vlc-1.2.0";
- // Set the vlc plugins directory path
- VlcContext.LibVlcPluginsPath = @".\plugins"; //@"C:\Program Files\VideoLAN\vlc-1.2.0\plugins";*/
- }
- /* Setting up the configuration of the VLC instance.
- * You can use any available command-line option using the AddOption function (see last two options).
- * A list of options is available at
- * http://wiki.videolan.org/VLC_command-line_help
- * for example. */
- //Set the startup options
- VlcContext.StartupOptions.IgnoreConfig = true;
- VlcContext.StartupOptions.LogOptions.LogInFile = true;
- // Shows the VLC log console (in addition to the applications window)
- VlcContext.StartupOptions.LogOptions.ShowLoggerConsole = false;
- VlcContext.StartupOptions.LogOptions.Verbosity = VlcLogVerbosities.None;
- // Pauses the playback of a movie on the last frame
- VlcContext.StartupOptions.AddOption("--play-and-pause");
- //VlcContext.StartupOptions.AddOption("--ffmpeg-hw");
- // Set the log level for the VLC instance
- //VlcContext.StartupOptions.LogOptions.Verbosity = VlcLogVerbosities.Debug;
- // Disable showing the movie file name as an overlay
- VlcContext.StartupOptions.AddOption("--video-title-show");
- VlcContext.StartupOptions.AddOption("--video-title-position=8");
- VlcContext.StartupOptions.AddOption("--video");
- VlcContext.StartupOptions.AddOption("--contrast=2");
- // Initialize the VlcContext
- if (!VlcContext.IsInitialized)
- {
- // Initialize the VlcContext
- VlcContext.Initialize();
- }
- }
- private void MainWindowOnClosing(object sender, CancelEventArgs e)
- {
- // Close the context.
- VlcContext.CloseAll();
- }
- #region Control playing
- private void ButtonPlayClick(object sender, RoutedEventArgs e)
- {
- myVlcControl.Play();
- }
- private void ButtonPauseClick(object sender, RoutedEventArgs e)
- {
- myVlcControl.Pause();
- }
- private void ButtonStopClick(object sender, RoutedEventArgs e)
- {
- myVlcControl.Stop();
- sliderPosition.Value = 0;
- }
- private void ButtonOpenClick(object sender, RoutedEventArgs e)
- {
- // myVlcControl.Stop();
- if (myVlcControl.Media != null)
- {
- myVlcControl.Media.ParsedChanged -= MediaOnParsedChanged;
- }
- var openFileDialog = new OpenFileDialog
- {
- Title = "Open media file for playback",
- FileName = "Media File",
- Filter = "All files |*.*"
- };
- if (openFileDialog.ShowDialog() != true)
- return;
- //textBlockOpen.Visibility = Visibility.Collapsed;
- myVlcControl.Media = new PathMedia(openFileDialog.FileName);
- myVlcControl.Media.ParsedChanged += MediaOnParsedChanged;
- myVlcControl.Play();
- }
- private void SliderVolumeValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
- {
- myVlcControl.AudioProperties.Volume = Convert.ToInt32(sliderVolume.Value);
- }
- private void CheckboxMuteCheckedChanged(object sender, RoutedEventArgs e)
- {
- myVlcControl.AudioProperties.IsMute = checkboxMute.IsChecked == true;
- }
- #endregion
- private void MediaOnParsedChanged(MediaBase sender, VlcEventArgs<int> e)
- {
- Action action = () =>
- {
- textBlock.Text = string.Format(
- "Duration: {0:00}:{1:00}:{2:00}",
- myVlcControl.Media.Duration.Hours,
- myVlcControl.Media.Duration.Minutes,
- myVlcControl.Media.Duration.Seconds);
- sliderVolume.Value = myVlcControl.AudioProperties.Volume;
- checkboxMute.IsChecked = myVlcControl.AudioProperties.IsMute;
- };
- this.Dispatcher.Invoke(action);
- }
- private void VlcControlOnPositionChanged(VlcControl sender, VlcEventArgs<float> e)
- {
- if (positionChanging)
- {
- return;
- }
- Action action = () =>
- {
- sliderPosition.Value = e.Data;
- };
- this.Dispatcher.Invoke(action);
- }
- private void VlcControlOnTimeChanged(VlcControl sender, VlcEventArgs<TimeSpan> e)
- {
- try
- {
- var duration = myVlcControl.Media.Duration;
- Action action = () =>
- {
- textBlock.Text = string.Format(
- "{0:00}:{1:00}:{2:00} / {3:00}:{4:00}:{5:00}",
- e.Data.Hours,
- e.Data.Minutes,
- e.Data.Seconds,
- duration.Hours,
- duration.Minutes,
- duration.Seconds);
- };
- this.Dispatcher.Invoke(action);
- }
- catch { }
- }
- #region Změna Pozice
- private void SliderMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
- {
- positionChanging = true;
- myVlcControl.PositionChanged -= VlcControlOnPositionChanged;
- }
- private void SliderMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
- {
- myVlcControl.Position = (float)sliderPosition.Value;
- myVlcControl.PositionChanged += VlcControlOnPositionChanged;
- positionChanging = false;
- }
- private void SliderValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
- {
- if (positionChanging)
- {
- myVlcControl.Position = (float)e.NewValue;
- }
- //Update the current position text when it is in pause
- var duration = myVlcControl.Media == null ? TimeSpan.Zero : myVlcControl.Media.Duration;
- var time = TimeSpan.FromMilliseconds(duration.TotalMilliseconds * myVlcControl.Position);
- textBlock.Text = string.Format(
- "{0:00}:{1:00}:{2:00} / {3:00}:{4:00}:{5:00}",
- time.Hours,
- time.Minutes,
- time.Seconds,
- duration.Hours,
- duration.Minutes,
- duration.Seconds);
- }
- #endregion
- private void sliProgress_DragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e)
- {
- positionChanging = true;
- }
- private void sliProgress_DragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
- {
- positionChanging = false;
- }
- private void ButtonPlayYoutubeSample(object sender, RoutedEventArgs e)
- {
- myVlcControl.Stop();
- if (myVlcControl.Media != null)
- {
- myVlcControl.Media.ParsedChanged -= MediaOnParsedChanged;
- }
- textBlockOpen.Visibility = Visibility.Collapsed;
- myVlcControl.Media = new LocationMedia("https://www.youtube.com/watch?v=RxabLA7UQ9k");
- myVlcControl.Media.ParsedChanged += MediaOnParsedChanged;
- myVlcControl.Play();
- }
- private void ButtonRate(object sender, RoutedEventArgs e)
- {
- myVlcControl.Rate = 2.0f;
- }
- private void ButtonDeafaultRate(object sender, RoutedEventArgs e)
- {
- myVlcControl.Rate = 1.0f;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement