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

1

10

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.

Trujillo answered 19/5, 2017 at 3:38 Comment(4)
You mean, a trackbar under the video to move, take forward the video ???? like in activeX plugin ?Anne
yes. But i am not using activeX plugin and rather i am using dot net vlc nuget package.Trujillo
Did you solve your problem? I did.Anne
@prempattnaik hi. prem would you mind to upload and share it? because i have problem with axvlc, when i try to vlc dot net form library, i can't display the player to the form.. many thanksPomiculture
A
11

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; 
   }));
Anne answered 24/5, 2017 at 12:1 Comment(2)
okay. Here m_mediaControl1 is like vlcControl1, But what is m_MediaPlayer ? How you initialized that ? Because i am using like vlcControl1.Time = trackBar1.value * 1000; and i can't move the trackbar at allTrujillo
yes, u r right. I updated codes. You have to make public " private VlcMediaPlayer myVlcMediaPlayer" which is under VlcControl.cs class. After that, you can access "myVlcControl.myVlcMediaPlayer.Time". and Don't use vlcControl1.Time...Anne

© 2022 - 2024 — McMap. All rights reserved.