Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a seekbar in VLC dot net forms in c# windows forms application

I need to add a custom seekbar / trackbar as you may say in c# windows forms. But the issue is there is almost no documentation on vlc dot net forms library. I need to know how to add a custom seekbar in windows forms application.

remember, i am not using vlc activeX plugin*. **Rather i am using nuget package of dot net library of vlc and everything is working fine. I have added toggle play and pause button, stop button, able to get current time, able to get total time and everything else. But i have no idea how to add a seekbar so that when i seek, the video moves to that position. Please help me with full code.

like image 819
prem pattnaik Avatar asked May 19 '17 03:05

prem pattnaik


1 Answers

I successfully finish, thank you it was a good practice for me. I've added the media in formdeneme() method

You have to make public the object which is in VlcControl.cs class.(private VlcMediaPlayer myVlcMediaPlayer;){Very important}

public int a = 0 ;` 
public int c = 0;  
public delegate void UpdateControlsDelegate(); //Execute when video loads

public formdeneme()
{
    InitializeComponent();
    myVlcControl.Play("file:///C:/Users/1315k/Downloads/machine.mp4");           
    // You can add your media like above.
    //Event handler for 'current media time' label
    this.vlcControl1.PositionChanged += new System.EventHandler<Vlc.DotNet.Core.VlcMediaPlayerPositionChangedEventArgs>(this.vlcControl1_PositionChanged);
    //Event handler for setting trackBar1.Maximum on media load
    vlcControl1.Playing += new System.EventHandler<VlcMediaPlayerPlayingEventArgs>(SetProgresMax);
}

// This is the main function which you looking.
private void trackBar1_Scroll(object sender, EventArgs e)
{                        
    myVlcControl.myVlcMediaPlayer.Time = trackBar1.Value * 1000;
    int b = (int)myVlcControl.myVlcMediaPlayer.Time / 1000;
    int d = b / 60;
    b = b - d * 60;
    label1.Text = d+":"+b + "/"+ c + ":" + a;
    // The Time value is milisecond, you have divide 1000 for be second.
}

private void formdeneme_Load(object sender, EventArgs e)
{
    a = (int)myVlcControl.myVlcMediaPlayer.Length / 1000;           
    trackBar1.Maximum = a;  
    c = a / 60;
    a = a - c * 60;        
    label1.Text = 0 + "/" + c+":"+a;            
}

You can add a button that can change media and trackbar.Maximum value.

UPDATED

Thanks to askepott He added some codes below, I didn't try but Looks good to me.

In order to have a label that displays the current media time, add this delegate function, it's called function (currentTrackTime) below and declaration at the top of this post. Also, don't forget to add the vlcControl1_PositionChanged event handler at the top.

//Update current video time label (delegate)

public void InvokeUpdateControls()
    {
        if (this.InvokeRequired)


        {
            this.Invoke(new UpdateControlsDelegate(currentTrackTime));
        }
        else
        {
            currentTrackTime();
        }
    }

//Update current video time label

private void currentTrackTime()
   {                        
   int b = (int)vlcControl1.VlcMediaPlayer.Time / 1000;
   int d = b / 60;
   b = b - d * 60;
   label1.Text = d+":"+b + "/"+ c + ":" + a; //min : sec / 
   }

//Add this to currentTrackTime() if you want your trackbar to automatically update it's value based on current media position

trackBar1.Value = b;

//Invoke update controls on video position change

private void vlcControl1_PositionChanged(object sender, Vlc.DotNet.Core.VlcMediaPlayerPositionChangedEventArgs e)
{
    InvokeUpdateControls();
}

//Furthermore, in case you have trouble getting and setting the vlcControl1.Length when loading the video, use this:

//Fire event when the video starts
private void SetProgresMax(object sender, VlcMediaPlayerPlayingEventArgs e)
{
   Invoke(new Action(() =>
   {
       trackBar1.Value = trackBar1.Minimum;
       var vlc = (VlcControl)sender;
       trackBar1.Maximum = (int)vlc.Length / 1000;
       a = (int)vlc.Length / 1000; // Length (s)
       c = a / 60; // Length (m)
       a = a % 60; // Length (s)
       label1.Text = 0 + "/" + c+":"+a; 
   }));
like image 150
Salih Karagoz Avatar answered Oct 21 '22 02:10

Salih Karagoz