Search code examples
c#.netlibvlc

Embedding LIb.VLC in winform Application


This Question is In reference with my previous question Embedding VLC player in WInform Application in .Net Core. Core.Intialize() Giving Exception

I want to run the player for certain time and during that time video should be on repeat. Currently code looks like this ...

Core.Initialize();
            var libvlc = new LibVLC();
            // Make VideoView control
            VideoView vv = new VideoView();
            vv.MediaPlayer = new MediaPlayer(libvlc);
            vv.Dock = DockStyle.Fill;
            // Add it to the form
            Controls.Add(vv);

            var uri = new Uri(@"C:\vid.3gp");
            // Use command line options as Options for media playback (https://wiki.videolan.org/VLC_command-line_help/)
            var media = new Media(libvlc, uri, ":input-repeat=65535");
            vv.MediaPlayer.Play(media);

            //Set fullscreen
            this.FormBorderStyle = FormBorderStyle.None;
            this.Size = Screen.PrimaryScreen.Bounds.Size;
            this.Location = Screen.PrimaryScreen.Bounds.Location;

How I can close the player after certain time. currently even if I close the form with the player video keeps playing in background till I close the whole application.

Just to inform the this winform application is created in .netcore3.1.

Regards.


Solution

  • Create MediaPlayer as class field and call it to start/pause/stop it in you WinForm application.

    private LibVLC _libVlc;
    private MediaPlayer _mediaPlayer;
    ...
    
    // Call this method in your constructor/initializer
    private void StartMediaPlayer(string videoUrl)
    {
        using var media = new Media(_libVlc, new Uri(videoUrl), ":input-repeat=65535");
        _mediaPlayer = new MediaPlayer(_libVlc)
        {
            Media = media
        };
    
        _mediaPlayer.Play();
    }
    
    // Method to stop media player
    private void button1_Click(object sender, EventArgs e)
    {
        _mediaPlayer.Stop();
    }