如何在c#windows窗体应用程序

时间:2017-05-19 03:38:23

标签: c# .net winforms vlc libvlc

我需要在c#windows窗体中添加自定义搜索栏/轨道栏。但问题是vlc dot net forms库几乎没有文档。我需要知道如何在Windows窗体应用程序中添加自定义搜索条。

请记住,我没有使用vlc activeX插件*。 **相反,我使用的是vlc的网络库的nuget包,一切正常。我添加了切换播放和暂停按钮,停止按钮,能够获得当前时间,能够获得总时间和其他所有内容。但我不知道如何添加搜索栏,以便在我寻找时,视频移动到该位置。 请使用完整的代码帮助我。

1 个答案:

答案 0 :(得分:4)

我成功完成了,谢谢你这对我来说是一个很好的做法。我在formdeneme()方法中添加了媒体

你必须公开VlcControl.cs类中的对象。(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;            
}

您可以添加一个可以更改媒体和轨迹栏的按钮。最大值。

<强>已更新

感谢 askepott 他在下面添加了一些代码,我没试过,但对我来说很好看。

为了有一个显示当前媒体时间的标签,添加这个委托函数,它在下面称为函数(currentTrackTime),并在此帖子的顶部添加声明。另外,不要忘记在顶部添加vlcControl1_PositionChanged事件处理程序。

//更新当前视频时间标签(委托)

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


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

//更新当前视频时间标签

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 / 
   }

//如果您希望轨迹栏根据当前媒体位置自动更新其值,请将其添加到currentTrackTime()

trackBar1.Value = b;

//调用视频位置更改的更新控件

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

//此外,如果您在加载视频时遇到并设置vlcControl1.Length时遇到问题,请使用:

//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; 
   }));