Advertisement
ORRNY66

VlcPlayer

Aug 13th, 2015
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.70 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.IO;
  15. #region Vlc
  16. using Microsoft.Win32;
  17. using Vlc.DotNet.Core;
  18. using Vlc.DotNet.Core.Interops.Signatures.LibVlc.Media;
  19. using Vlc.DotNet.Core.Interops.Signatures.LibVlc.MediaListPlayer;
  20. using Vlc.DotNet.Core.Medias;
  21. using Vlc.DotNet.Wpf;
  22. using System.ComponentModel;
  23.  
  24. #endregion
  25. namespace VLC_wpf
  26. {
  27.     /// <summary>
  28.     /// Interaction logic for MainWindow.xaml
  29.     /// </summary>
  30.     public partial class MainWindow : Window
  31.     {
  32.         private bool positionChanging;
  33.  
  34.         public MainWindow()
  35.         {
  36.  
  37.             initVLCPlayer();
  38.             InitializeComponent();
  39.             sliderPosition.ApplyTemplate();
  40.  
  41.             System.Windows.Controls.Primitives.Thumb thumb = (sliderPosition.Template.FindName(
  42.  
  43.                 "PART_Track", sliderPosition) as System.Windows.Controls.Primitives.Track).Thumb;
  44.  
  45.             thumb.MouseEnter
  46.  
  47.                 += new MouseEventHandler(thumb_MouseEnter);
  48.  
  49.             Closing += MainWindowOnClosing;
  50.         }
  51.         private void thumb_MouseEnter(object sender, MouseEventArgs e)
  52.         {
  53.  
  54.             if (e.LeftButton == MouseButtonState.Pressed
  55.  
  56.                 && e.MouseDevice.Captured == null)
  57.             {
  58.                 MouseButtonEventArgs args = new MouseButtonEventArgs(
  59.  
  60.                     e.MouseDevice, e.Timestamp, MouseButton.Left);
  61.  
  62.                 args.RoutedEvent = MouseLeftButtonDownEvent;
  63.  
  64.                 (sender as System.Windows.Controls.Primitives.Thumb).RaiseEvent(args);
  65.  
  66.             }
  67.         }
  68.      
  69.         private void initVLCPlayer()
  70.         {
  71.  
  72.             /*if (Directory.Exists("C:\\Program Files\\VideoLAN\\VLC"))
  73.             {
  74.                 // Set libvlc.dll and libvlccore.dll directory path
  75.                 VlcContext.LibVlcDllsPath = @"C:\Program Files\VideoLAN\VLC";
  76.                 // Set the vlc plugins directory path
  77.                 VlcContext.LibVlcPluginsPath = @"C:\Program Files\VideoLAN\VLC\plugins";
  78.             }
  79.             else
  80.             {
  81.                 // Set libvlc.dll and libvlccore.dll directory path
  82.                 VlcContext.LibVlcDllsPath = @"C:\Program Files (x86)\VideoLAN\VLC";
  83.                 // Set the vlc plugins directory path
  84.                 VlcContext.LibVlcPluginsPath = @"C:\Program Files (x86)\VideoLAN\VLC\plugins";
  85.             }*/
  86.  
  87.  
  88.             if (Environment.Is64BitOperatingSystem)
  89.             {
  90.                 VlcContext.LibVlcDllsPath = CommonStrings.LIBVLC_DLLS_PATH_DEFAULT_VALUE_AMD64;
  91.                 VlcContext.LibVlcPluginsPath = CommonStrings.PLUGINS_PATH_DEFAULT_VALUE_AMD64;
  92.  
  93.                 /*// Set libvlc.dll and libvlccore.dll directory path
  94.                 VlcContext.LibVlcDllsPath = @".\x86"; //@"C:\Program Files\VideoLAN\vlc-2.0.5";
  95.  
  96.                 // Set the vlc plugins directory path
  97.                 VlcContext.LibVlcPluginsPath = @".\plugins"; //@"C:\Program Files\VideoLAN\vlc-2.0.5\plugins";*/
  98.  
  99.             }
  100.             else
  101.             {
  102.                 VlcContext.LibVlcDllsPath = CommonStrings.LIBVLC_DLLS_PATH_DEFAULT_VALUE_X86;
  103.                 VlcContext.LibVlcPluginsPath = CommonStrings.PLUGINS_PATH_DEFAULT_VALUE_X86;
  104.  
  105.                 /*// Set libvlc.dll and libvlccore.dll directory path
  106.                 VlcContext.LibVlcDllsPath = @".\x64"; //@"C:\Program Files\VideoLAN\vlc-1.2.0";
  107.  
  108.                 // Set the vlc plugins directory path
  109.                 VlcContext.LibVlcPluginsPath = @".\plugins"; //@"C:\Program Files\VideoLAN\vlc-1.2.0\plugins";*/
  110.             }
  111.  
  112.             /* Setting up the configuration of the VLC instance.
  113.             * You can use any available command-line option using the AddOption function (see last two options).
  114.             * A list of options is available at
  115.             *     http://wiki.videolan.org/VLC_command-line_help
  116.             * for example. */
  117.  
  118.             //Set the startup options
  119.             VlcContext.StartupOptions.IgnoreConfig = true;
  120.             VlcContext.StartupOptions.LogOptions.LogInFile = true;
  121.             // Shows the VLC log console (in addition to the applications window)
  122.             VlcContext.StartupOptions.LogOptions.ShowLoggerConsole = false;
  123.             VlcContext.StartupOptions.LogOptions.Verbosity = VlcLogVerbosities.None;
  124.  
  125.             // Pauses the playback of a movie on the last frame
  126.             VlcContext.StartupOptions.AddOption("--play-and-pause");
  127.  
  128.             //VlcContext.StartupOptions.AddOption("--ffmpeg-hw");
  129.  
  130.             // Set the log level for the VLC instance
  131.             //VlcContext.StartupOptions.LogOptions.Verbosity = VlcLogVerbosities.Debug;
  132.  
  133.             // Disable showing the movie file name as an overlay
  134.             VlcContext.StartupOptions.AddOption("--video-title-show");
  135.             VlcContext.StartupOptions.AddOption("--video-title-position=8");
  136.  
  137.             VlcContext.StartupOptions.AddOption("--video");
  138.             VlcContext.StartupOptions.AddOption("--contrast=2");
  139.  
  140.  
  141.             // Initialize the VlcContext
  142.             if (!VlcContext.IsInitialized)
  143.             {
  144.                 // Initialize the VlcContext
  145.                 VlcContext.Initialize();
  146.             }
  147.         }
  148.      
  149.         private void MainWindowOnClosing(object sender, CancelEventArgs e)
  150.         {
  151.             // Close the context.
  152.             VlcContext.CloseAll();
  153.         }
  154.         #region Control playing
  155.  
  156.         private void ButtonPlayClick(object sender, RoutedEventArgs e)
  157.         {
  158.             myVlcControl.Play();
  159.         }
  160.  
  161.    
  162.         private void ButtonPauseClick(object sender, RoutedEventArgs e)
  163.         {
  164.             myVlcControl.Pause();
  165.         }
  166.  
  167.        
  168.         private void ButtonStopClick(object sender, RoutedEventArgs e)
  169.         {
  170.             myVlcControl.Stop();
  171.             sliderPosition.Value = 0;
  172.         }
  173.  
  174.         private void ButtonOpenClick(object sender, RoutedEventArgs e)
  175.         {
  176.            // myVlcControl.Stop();
  177.  
  178.             if (myVlcControl.Media != null)
  179.             {
  180.                 myVlcControl.Media.ParsedChanged -= MediaOnParsedChanged;
  181.             }
  182.  
  183.             var openFileDialog = new OpenFileDialog
  184.             {
  185.                 Title = "Open media file for playback",
  186.                 FileName = "Media File",
  187.                 Filter = "All files |*.*"
  188.             };
  189.  
  190.            
  191.             if (openFileDialog.ShowDialog() != true)
  192.                 return;
  193.  
  194.             //textBlockOpen.Visibility = Visibility.Collapsed;
  195.  
  196.             myVlcControl.Media = new PathMedia(openFileDialog.FileName);
  197.             myVlcControl.Media.ParsedChanged += MediaOnParsedChanged;
  198.             myVlcControl.Play();
  199.  
  200.      
  201.         }
  202.  
  203.      
  204.         private void SliderVolumeValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
  205.         {
  206.             myVlcControl.AudioProperties.Volume = Convert.ToInt32(sliderVolume.Value);
  207.         }
  208.  
  209.        
  210.         private void CheckboxMuteCheckedChanged(object sender, RoutedEventArgs e)
  211.         {
  212.             myVlcControl.AudioProperties.IsMute = checkboxMute.IsChecked == true;
  213.         }
  214.  
  215.  
  216.         #endregion
  217.         private void MediaOnParsedChanged(MediaBase sender, VlcEventArgs<int> e)
  218.         {
  219.             Action action = () =>
  220.             {
  221.                 textBlock.Text = string.Format(
  222.                     "Duration: {0:00}:{1:00}:{2:00}",
  223.                     myVlcControl.Media.Duration.Hours,
  224.                     myVlcControl.Media.Duration.Minutes,
  225.                     myVlcControl.Media.Duration.Seconds);
  226.  
  227.                 sliderVolume.Value = myVlcControl.AudioProperties.Volume;
  228.                 checkboxMute.IsChecked = myVlcControl.AudioProperties.IsMute;
  229.             };
  230.  
  231.             this.Dispatcher.Invoke(action);
  232.         }
  233.         private void VlcControlOnPositionChanged(VlcControl sender, VlcEventArgs<float> e)
  234.         {
  235.             if (positionChanging)
  236.             {
  237.                
  238.                 return;
  239.             }
  240.  
  241.             Action action = () =>
  242.             {
  243.                 sliderPosition.Value = e.Data;
  244.             };
  245.  
  246.             this.Dispatcher.Invoke(action);
  247.         }
  248.  
  249.         private void VlcControlOnTimeChanged(VlcControl sender, VlcEventArgs<TimeSpan> e)
  250.         {
  251.             try
  252.             {
  253.                 var duration = myVlcControl.Media.Duration;
  254.  
  255.                 Action action = () =>
  256.                 {
  257.                     textBlock.Text = string.Format(
  258.                         "{0:00}:{1:00}:{2:00} / {3:00}:{4:00}:{5:00}",
  259.                         e.Data.Hours,
  260.                         e.Data.Minutes,
  261.                         e.Data.Seconds,
  262.                         duration.Hours,
  263.                         duration.Minutes,
  264.                         duration.Seconds);
  265.                 };
  266.                 this.Dispatcher.Invoke(action);
  267.             }
  268.             catch { }
  269.         }
  270.         #region ZmΔ›na Pozice
  271.         private void SliderMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  272.         {
  273.             positionChanging = true;
  274.             myVlcControl.PositionChanged -= VlcControlOnPositionChanged;
  275.         }
  276.         private void SliderMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  277.         {
  278.             myVlcControl.Position = (float)sliderPosition.Value;
  279.             myVlcControl.PositionChanged += VlcControlOnPositionChanged;
  280.  
  281.             positionChanging = false;
  282.         }
  283.         private void SliderValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
  284.         {
  285.             if (positionChanging)
  286.             {
  287.                 myVlcControl.Position = (float)e.NewValue;
  288.             }
  289.             //Update the current position text when it is in pause
  290.             var duration = myVlcControl.Media == null ? TimeSpan.Zero : myVlcControl.Media.Duration;
  291.             var time = TimeSpan.FromMilliseconds(duration.TotalMilliseconds * myVlcControl.Position);
  292.             textBlock.Text = string.Format(
  293.                 "{0:00}:{1:00}:{2:00} / {3:00}:{4:00}:{5:00}",
  294.                 time.Hours,
  295.                 time.Minutes,
  296.                 time.Seconds,
  297.                 duration.Hours,
  298.                 duration.Minutes,
  299.                 duration.Seconds);
  300.         }
  301.  
  302.         #endregion
  303.         private void sliProgress_DragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e)
  304.         {
  305.            
  306.             positionChanging = true;
  307.        
  308.         }
  309.  
  310.         private void sliProgress_DragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
  311.         {
  312.        
  313.             positionChanging = false;
  314.         }
  315.        
  316.  
  317.         private void ButtonPlayYoutubeSample(object sender, RoutedEventArgs e)
  318.         {
  319.             myVlcControl.Stop();
  320.  
  321.             if (myVlcControl.Media != null)
  322.             {
  323.                 myVlcControl.Media.ParsedChanged -= MediaOnParsedChanged;
  324.             }
  325.  
  326.             textBlockOpen.Visibility = Visibility.Collapsed;
  327.  
  328.             myVlcControl.Media = new LocationMedia("https://www.youtube.com/watch?v=RxabLA7UQ9k");
  329.             myVlcControl.Media.ParsedChanged += MediaOnParsedChanged;
  330.             myVlcControl.Play();
  331.  
  332.         }
  333.  
  334.         private void ButtonRate(object sender, RoutedEventArgs e)
  335.         {
  336.             myVlcControl.Rate = 2.0f;
  337.         }
  338.  
  339.         private void ButtonDeafaultRate(object sender, RoutedEventArgs e)
  340.         {
  341.             myVlcControl.Rate = 1.0f;
  342.         }
  343.  
  344.      
  345.     }
  346. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement