0

I am hoping someone can help me in getting an issue of mine to work, I feel as if it is an easy one, however not having any luck in fixing what I am trying to do. I want to be able to pause a video which I am playing using vlc.dotnet below is a brief summary of the structure of my code.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Reflection;
using System.IO;
using Vlc.DotNet.Forms;
using System.Threading;
using Vlc.DotNet.Core;
using System.Diagnostics;

namespace TS1_C
{
 public partial class Form1 : Form
    {
 public Form1()
        {
            InitializeComponent();
           
        }

 private void Form1_Load(object sender, EventArgs e)
        {
          button8.Click += new EventHandler(this.button8_Click);
        }

void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
string chosen = listBox1.SelectedItem.ToString();
                    string final = selectedpath2 + "\\" + chosen;  //Path
 playfile(final);
}
 void playfile(string final)
        {
            var control = new VlcControl();
            var currentAssembly = Assembly.GetEntryAssembly();
            var currentDirectory = new FileInfo(currentAssembly.Location).DirectoryName;
            // Default installation path of VideoLAN.LibVLC.Windows
            var libDirectory = new DirectoryInfo(Path.Combine(currentDirectory, "libvlc", IntPtr.Size == 4 ? "win-x86" : "win-x64"));
            control.BeginInit();
            control.VlcLibDirectory = libDirectory;
            control.Dock = DockStyle.Fill;
            control.EndInit();
            panel1.Controls.Add(control);
            control.Play();
        }

 private void button8_Click(object sender, EventArgs e)
        {
           
        }

}
}

As you can see I have one method which takes a double click from an item in a list box and plays it using the method playfile. However I want to be able to pause the video using my button known as button8. I have tried many things even this

control.Paused += new System.EventHandler<VlcMediaPlayerPausedEventArgs>(button8_Click);

Which I put into the playfile method, however nothing seems to work. I am wondering if my whole method in which I play a file using playfile(); is completely wrong. I am hoping someone can help me in trying to achieve what I need

Thank you

Vijay Yadav
  • 91
  • 13

1 Answers1

0

Your control should be initialized only once:

private VlcControl control;

public Form1()
{
            InitializeComponent();
            control = new VlcControl();
            var currentAssembly = Assembly.GetEntryAssembly();
            var currentDirectory = new FileInfo(currentAssembly.Location).DirectoryName;
            // Default installation path of VideoLAN.LibVLC.Windows
            var libDirectory = new DirectoryInfo(Path.Combine(currentDirectory, "libvlc", IntPtr.Size == 4 ? "win-x86" : "win-x64"));
            control.BeginInit();
            control.VlcLibDirectory = libDirectory;
            control.Dock = DockStyle.Fill;
            control.EndInit();
            panel1.Controls.Add(control);
}

then, your play method could be simplified:

 void playfile(string url)
        {
            control.Play(url);
        }

And for your pause method:

 private void button8_Click(object sender, EventArgs e)
        {
           control.Pause();
        }
cube45
  • 3,429
  • 2
  • 24
  • 35