Advertisement
ORRNY66

Wpf VlcPlayer

Aug 12th, 2015
488
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 15.08 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.  
  33.         /// <summary>
  34.         /// Used to indicate that the user is currently changing the position (and the position bar shall not be updated).
  35.         /// </summary>
  36.         private bool positionChanging;
  37.  
  38.         public MainWindow()
  39.         {
  40.  
  41.             initVLCPlayer();
  42.             InitializeComponent();
  43.             sliderPosition.ApplyTemplate();
  44.  
  45.             System.Windows.Controls.Primitives.Thumb thumb = (sliderPosition.Template.FindName(
  46.  
  47.                 "PART_Track", sliderPosition) as System.Windows.Controls.Primitives.Track).Thumb;
  48.  
  49.             thumb.MouseEnter
  50.  
  51.                 += new MouseEventHandler(thumb_MouseEnter);
  52.  
  53.             Closing += MainWindowOnClosing;
  54.         }
  55.         private void thumb_MouseEnter(object sender, MouseEventArgs e)
  56.         {
  57.  
  58.             if (e.LeftButton == MouseButtonState.Pressed
  59.  
  60.                 && e.MouseDevice.Captured == null)
  61.             {
  62.  
  63.                 // the left button is pressed on mouse enter
  64.  
  65.                 // but the mouse isn't captured, so the thumb
  66.  
  67.                 // must have been moved under the mouse in response
  68.  
  69.                 // to a click on the track.
  70.  
  71.                 // Generate a MouseLeftButtonDown event.
  72.  
  73.                 MouseButtonEventArgs args = new MouseButtonEventArgs(
  74.  
  75.                     e.MouseDevice, e.Timestamp, MouseButton.Left);
  76.  
  77.                 args.RoutedEvent = MouseLeftButtonDownEvent;
  78.  
  79.                 (sender as System.Windows.Controls.Primitives.Thumb).RaiseEvent(args);
  80.  
  81.             }
  82.         }
  83.      
  84.         private void initVLCPlayer()
  85.         {
  86.  
  87.             /*if (Directory.Exists("C:\\Program Files\\VideoLAN\\VLC"))
  88.             {
  89.                 // Set libvlc.dll and libvlccore.dll directory path
  90.                 VlcContext.LibVlcDllsPath = @"C:\Program Files\VideoLAN\VLC";
  91.                 // Set the vlc plugins directory path
  92.                 VlcContext.LibVlcPluginsPath = @"C:\Program Files\VideoLAN\VLC\plugins";
  93.             }
  94.             else
  95.             {
  96.                 // Set libvlc.dll and libvlccore.dll directory path
  97.                 VlcContext.LibVlcDllsPath = @"C:\Program Files (x86)\VideoLAN\VLC";
  98.                 // Set the vlc plugins directory path
  99.                 VlcContext.LibVlcPluginsPath = @"C:\Program Files (x86)\VideoLAN\VLC\plugins";
  100.             }*/
  101.  
  102.  
  103.             if (Environment.Is64BitOperatingSystem)
  104.             {
  105.                 VlcContext.LibVlcDllsPath = CommonStrings.LIBVLC_DLLS_PATH_DEFAULT_VALUE_AMD64;
  106.                 VlcContext.LibVlcPluginsPath = CommonStrings.PLUGINS_PATH_DEFAULT_VALUE_AMD64;
  107.  
  108.                 /*// Set libvlc.dll and libvlccore.dll directory path
  109.                 VlcContext.LibVlcDllsPath = @".\x86"; //@"C:\Program Files\VideoLAN\vlc-1.2.0";
  110.  
  111.                 // Set the vlc plugins directory path
  112.                 VlcContext.LibVlcPluginsPath = @".\plugins"; //@"C:\Program Files\VideoLAN\vlc-1.2.0\plugins";*/
  113.  
  114.             }
  115.             else
  116.             {
  117.                 VlcContext.LibVlcDllsPath = CommonStrings.LIBVLC_DLLS_PATH_DEFAULT_VALUE_X86;
  118.                 VlcContext.LibVlcPluginsPath = CommonStrings.PLUGINS_PATH_DEFAULT_VALUE_X86;
  119.  
  120.                 /*// Set libvlc.dll and libvlccore.dll directory path
  121.                 VlcContext.LibVlcDllsPath = @".\x64"; //@"C:\Program Files\VideoLAN\vlc-1.2.0";
  122.  
  123.                 // Set the vlc plugins directory path
  124.                 VlcContext.LibVlcPluginsPath = @".\plugins"; //@"C:\Program Files\VideoLAN\vlc-1.2.0\plugins";*/
  125.             }
  126.  
  127.             /* Setting up the configuration of the VLC instance.
  128.             * You can use any available command-line option using the AddOption function (see last two options).
  129.             * A list of options is available at
  130.             *     http://wiki.videolan.org/VLC_command-line_help
  131.             * for example. */
  132.  
  133.             //Set the startup options
  134.             VlcContext.StartupOptions.IgnoreConfig = true;
  135.             VlcContext.StartupOptions.LogOptions.LogInFile = true;
  136.             // Shows the VLC log console (in addition to the applications window)
  137.             VlcContext.StartupOptions.LogOptions.ShowLoggerConsole = false;
  138.             VlcContext.StartupOptions.LogOptions.Verbosity = VlcLogVerbosities.None;
  139.  
  140.             // Pauses the playback of a movie on the last frame
  141.             VlcContext.StartupOptions.AddOption("--play-and-pause");
  142.  
  143.             VlcContext.StartupOptions.AddOption("--ffmpeg-hw");
  144.  
  145.             // Set the log level for the VLC instance
  146.             VlcContext.StartupOptions.LogOptions.Verbosity = VlcLogVerbosities.Debug;
  147.  
  148.             // Disable showing the movie file name as an overlay
  149.             VlcContext.StartupOptions.AddOption("--no-video-title-show");
  150.  
  151.             VlcContext.StartupOptions.AddOption(":transform-type={180}");
  152.  
  153.             // Initialize the VlcContext
  154.             if (!VlcContext.IsInitialized)
  155.             {
  156.                 // Initialize the VlcContext
  157.                 VlcContext.Initialize();
  158.             }
  159.         }
  160.         /// <summary>
  161.         /// Main window closing event
  162.         /// </summary>
  163.         /// <param name="sender">Event sender. </param>
  164.         /// <param name="e">Event arguments. </param>
  165.         private void MainWindowOnClosing(object sender, CancelEventArgs e)
  166.         {
  167.             // Close the context.
  168.             VlcContext.CloseAll();
  169.         }
  170.         #region Control playing
  171.  
  172.         /// <summary>
  173.         /// Called if the Play button is clicked; starts the VLC playback.
  174.         /// </summary>
  175.         /// <param name="sender">Event sender. </param>
  176.         /// <param name="e">Event arguments. </param>
  177.         private void ButtonPlayClick(object sender, RoutedEventArgs e)
  178.         {
  179.             myVlcControl.Play();
  180.         }
  181.  
  182.         /// <summary>
  183.         /// Called if the Pause button is clicked; pauses the VLC playback.
  184.         /// </summary>
  185.         /// <param name="sender">Event sender. </param>
  186.         /// <param name="e">Event arguments. </param>
  187.         private void ButtonPauseClick(object sender, RoutedEventArgs e)
  188.         {
  189.             myVlcControl.Pause();
  190.         }
  191.  
  192.         /// <summary>
  193.         /// Called if the Stop button is clicked; stops the VLC playback.
  194.         /// </summary>
  195.         /// <param name="sender">Event sender. </param>
  196.         /// <param name="e">Event arguments. </param>
  197.         private void ButtonStopClick(object sender, RoutedEventArgs e)
  198.         {
  199.             myVlcControl.Stop();
  200.             sliderPosition.Value = 0;
  201.         }
  202.  
  203.         /// <summary>
  204.         /// Called if the Open button is clicked; shows the open file dialog to select a media file to play.
  205.         /// </summary>
  206.         /// <param name="sender">Event sender. </param>
  207.         /// <param name="e">Event arguments. </param>
  208.         private void ButtonOpenClick(object sender, RoutedEventArgs e)
  209.         {
  210.            // myVlcControl.Stop();
  211.  
  212.             if (myVlcControl.Media != null)
  213.             {
  214.                 myVlcControl.Media.ParsedChanged -= MediaOnParsedChanged;
  215.             }
  216.  
  217.             var openFileDialog = new OpenFileDialog
  218.             {
  219.                 Title = "Open media file for playback",
  220.                 FileName = "Media File",
  221.                 Filter = "All files |*.*"
  222.             };
  223.  
  224.             // Process open file dialog box results
  225.             if (openFileDialog.ShowDialog() != true)
  226.                 return;
  227.  
  228.             //textBlockOpen.Visibility = Visibility.Collapsed;
  229.  
  230.             myVlcControl.Media = new PathMedia(openFileDialog.FileName);
  231.             myVlcControl.Media.ParsedChanged += MediaOnParsedChanged;
  232.             myVlcControl.Play();
  233.  
  234.             /* Instead of opening a file for playback you can also connect to media streams using
  235.              *     myVlcControl.Media = new LocationMedia(@"http://88.190.232.102:6404");
  236.              *     myVlcControl.Play();
  237.              */
  238.         }
  239.  
  240.         /// <summary>
  241.         /// Volume value changed by the user.
  242.         /// </summary>
  243.         /// <param name="sender">Event sender. </param>
  244.         /// <param name="e">Event arguments. </param>
  245.         private void SliderVolumeValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
  246.         {
  247.             myVlcControl.AudioProperties.Volume = Convert.ToInt32(sliderVolume.Value);
  248.         }
  249.  
  250.         /// <summary>
  251.         /// Mute audio check changed
  252.         /// </summary>
  253.         /// <param name="sender">Event sender. </param>
  254.         /// <param name="e">Event arguments. </param>
  255.         private void CheckboxMuteCheckedChanged(object sender, RoutedEventArgs e)
  256.         {
  257.             myVlcControl.AudioProperties.IsMute = checkboxMute.IsChecked == true;
  258.         }
  259.  
  260.  
  261.         #endregion
  262.         /// <summary>
  263.         /// Called by <see cref="VlcControl.Media"/> when the media information was parsed.
  264.         /// </summary>
  265.         /// <param name="sender">Event sending media. </param>
  266.         /// <param name="e">VLC event arguments. </param>
  267.         private void MediaOnParsedChanged(MediaBase sender, VlcEventArgs<int> e)
  268.         {
  269.             Action action = () =>
  270.             {
  271.                 textBlock.Text = string.Format(
  272.                     "Duration: {0:00}:{1:00}:{2:00}",
  273.                     myVlcControl.Media.Duration.Hours,
  274.                     myVlcControl.Media.Duration.Minutes,
  275.                     myVlcControl.Media.Duration.Seconds);
  276.  
  277.                 sliderVolume.Value = myVlcControl.AudioProperties.Volume;
  278.                 checkboxMute.IsChecked = myVlcControl.AudioProperties.IsMute;
  279.             };
  280.  
  281.             this.Dispatcher.Invoke(action);
  282.         }
  283.         /// <summary>
  284.         /// Called by the <see cref="VlcControl"/> when the media position changed during playback.
  285.         /// </summary>
  286.         /// <param name="sender">Event sennding control. </param>
  287.         /// <param name="e">VLC event arguments. </param>
  288.         private void VlcControlOnPositionChanged(VlcControl sender, VlcEventArgs<float> e)
  289.         {
  290.             if (positionChanging)
  291.             {
  292.                 // User is currently changing the position using the slider, so do not update.
  293.                 return;
  294.             }
  295.  
  296.             Action action = () =>
  297.             {
  298.                 sliderPosition.Value = e.Data;
  299.             };
  300.  
  301.             this.Dispatcher.Invoke(action);
  302.         }
  303.  
  304.         private void VlcControlOnTimeChanged(VlcControl sender, VlcEventArgs<TimeSpan> e)
  305.         {
  306.             var duration = myVlcControl.Media.Duration;
  307.  
  308.             Action action = () =>
  309.             {
  310.                 textBlock.Text = string.Format(
  311.                     "{0:00}:{1:00}:{2:00} / {3:00}:{4:00}:{5:00}",
  312.                     e.Data.Hours,
  313.                     e.Data.Minutes,
  314.                     e.Data.Seconds,
  315.                     duration.Hours,
  316.                     duration.Minutes,
  317.                     duration.Seconds);
  318.             };
  319.             this.Dispatcher.Invoke(action);
  320.         }
  321.         #region Change position
  322.  
  323.         /// <summary>
  324.         /// Start position changing, prevents updates for the slider by the player.
  325.         /// </summary>
  326.         /// <param name="sender">Event sender. </param>
  327.         /// <param name="e">Event arguments. </param>
  328.         private void SliderMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  329.         {
  330.             positionChanging = true;
  331.             myVlcControl.PositionChanged -= VlcControlOnPositionChanged;
  332.         }
  333.  
  334.         /// <summary>
  335.         /// Stop position changing, re-enables updates for the slider by the player.
  336.         /// </summary>
  337.         /// <param name="sender">Event sender. </param>
  338.         /// <param name="e">Event arguments. </param>
  339.         private void SliderMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  340.         {
  341.             myVlcControl.Position = (float)sliderPosition.Value;
  342.             myVlcControl.PositionChanged += VlcControlOnPositionChanged;
  343.  
  344.             positionChanging = false;
  345.         }
  346.  
  347.         /// <summary>
  348.         /// Change position when the slider value is updated.
  349.         /// </summary>
  350.         /// <param name="sender">Event sender. </param>
  351.         /// <param name="e">Event arguments. </param>
  352.         private void SliderValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
  353.         {
  354.             if (positionChanging)
  355.             {
  356.                 myVlcControl.Position = (float)e.NewValue;
  357.             }
  358.             //Update the current position text when it is in pause
  359.             var duration = myVlcControl.Media == null ? TimeSpan.Zero : myVlcControl.Media.Duration;
  360.             var time = TimeSpan.FromMilliseconds(duration.TotalMilliseconds * myVlcControl.Position);
  361.             textBlock.Text = string.Format(
  362.                 "{0:00}:{1:00}:{2:00} / {3:00}:{4:00}:{5:00}",
  363.                 time.Hours,
  364.                 time.Minutes,
  365.                 time.Seconds,
  366.                 duration.Hours,
  367.                 duration.Minutes,
  368.                 duration.Seconds);
  369.         }
  370.  
  371.         #endregion
  372.         private void sliProgress_DragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e)
  373.         {
  374.            
  375.             positionChanging = true;
  376.        
  377.         }
  378.  
  379.         private void sliProgress_DragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
  380.         {
  381.        
  382.             positionChanging = false;
  383.         }
  384.        
  385.  
  386.         private void ButtonPlayYoutubeSample(object sender, RoutedEventArgs e)
  387.         {
  388.             myVlcControl.Stop();
  389.  
  390.             if (myVlcControl.Media != null)
  391.             {
  392.                 myVlcControl.Media.ParsedChanged -= MediaOnParsedChanged;
  393.             }
  394.  
  395.             textBlockOpen.Visibility = Visibility.Collapsed;
  396.  
  397.             myVlcControl.Media = new LocationMedia("https://www.youtube.com/watch?v=RxabLA7UQ9k");
  398.             myVlcControl.Media.ParsedChanged += MediaOnParsedChanged;
  399.             myVlcControl.Play();
  400.  
  401.         }
  402.  
  403.         private void ButtonRate(object sender, RoutedEventArgs e)
  404.         {
  405.             myVlcControl.Rate = 2.0f;
  406.         }
  407.  
  408.         private void ButtonDeafaultRate(object sender, RoutedEventArgs e)
  409.         {
  410.             //myVlcControl.Rate = 1.0f;
  411.             VlcContext.StartupOptions.AddOption(":transform-type={180}");
  412.         }
  413.  
  414.      
  415.     }
  416. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement