Using Vlc.DotNet in WinForms - ZeBobo5/Vlc.DotNet GitHub Wiki
#Using Vlc.DotNet in WinForms
Since there very little documentation on this I am slapped together a quick little page on here to let people know how to use this. I am using Visual Studio 2017 coding in VB.Net and these are the exact instructions I did in order to get VLC.DotNet to play a sound file:
- Open "Manage NuGet Packages"
- Search "VLC.DotNet"
- Install all 4 (VLC.DotNet.Forms, VLC.DotNet.Core.Interops, VLC.DotNet.Core, and VLC.DotNet.Wpf)
- Go to your design view, right click on your Toolbox, and select "Choose Items..."
- Under the ".Net Framework Components" tab click "Browse..."
- Navigate to your current projects directory (e.g. C:\Users[username]\Documents\Visual Studio\Projects\WindowsApplication1)
- Go inside the "packages" folder
- Go inside the "Vlc.DotNet.Forms.X.X.X\lib" folders. Choose your proper framework library (I am trying with net45/AnyCPU)
- Verify "VlcControl" is checked in the list and click Ok
- Go to your Toolbox and find the VlcControl. Add it to your form
- Note: I have seen many references to some C# initialization code to get this to work (example 1, 2, 3, 4). I have no idea how to do that as I was unable to find those classes.
- If you can't get it to work using that method, simply go to the Properties of the VlcControl1. Go down to VlcLibDirectory, click the ellipse, browse to your VLC folder (It will either be "C:\Program Files (x86)\VideoLAN\VLC" or C:\Program Files\VideoLAN\VLC"). Once you finish you can run the program and use VLC in dot net!
Sorry for the crappy wiki page, but I thought this information was important to share
Quick and easy code for VB.Net:
'Play a song
VlcControl1.SetMedia(New IO.FileInfo("C:\Users\Bob\Music\MyAwesomeSong.mp3"))
VlcControl1.Play()
'Change/get the volume
VlcControl1.Audio.Volume = Num
'Change/get the position
VlcControl1.Position = Num
'Get the number of seconds in the song
VlcControl1.GetCurrentMedia.Duration.TotalSeconds
'Pause and stop
VlcControl1.Pause()
VlcControl1.Stop()
That should be enough to get you started
Some additional info:
You need to set the path to the correct VLC installation. This could be done in 2 ways:
-
with the VlcLibDirectory in the object at design time. NOTE: the setting cannot be cleared. You have to destroy and re create the object. Also note that this could make a mess on x86/x64 systems as the Program Files could be x86/x64 while the library works only in x86 and with x86 VLC lib.
-
at runtime using the events. At design time you have to set the VlcLibDirectoryNeeded event of the control to a procedure like this one:
Private Sub checkdir2(sender As Object, e As Forms.VlcLibDirectoryNeededEventArgs) Handles VlcControl1.VlcLibDirectoryNeeded Dim aP As String If Environment.Is64BitOperatingSystem Then aP = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), "VideoLAN\VLC") Else aP = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "VideoLAN\VLC") End If If Not File.Exists(Path.Combine(aP, "libvlc.dll")) Then Using fbdDialog As New FolderBrowserDialog() fbdDialog.Description = "Select VLC Path" fbdDialog.SelectedPath = Path.Combine(aP, "VideoLAN\VLC") If fbdDialog.ShowDialog() = DialogResult.OK Then e.VlcLibDirectory = New DirectoryInfo(fbdDialog.SelectedPath) End If End Using Else e.VlcLibDirectory = New DirectoryInfo(aP) End If End Sub
Dynamic creation
Once you have created the object and set all properties and event handlers you must call EndInit() that will perform initialization and will invoke vlclib
How to add Vlc control to the toolbox
At first right click on an empty space on the toolbox and click choose item
.