C winform Plays Multiple Videos Loop

  • 2021-12-09 09:48:05
  • OfStack

In this paper, we share the specific codes of winform cycling to play multiple videos for your reference. The specific contents are as follows

Environment: vs2015 + winform

First of all, it is very convenient for vs to bring its own components. Therefore, with windowMediaplayer components, if you play a single loop, add an attribute:


axWindowsMediaPlayer1.settings.autoStart = true;      // Setting AutoPlay 
axWindowsMediaPlayer1.settings.setMode("loop", true);   // Set up loop playback 

To get down to business:

1: Drag in components button, windowMediaplayer, listbox, timer

2:


 List<string> fileList = new List<string>();   
    private void button1_Click(object sender, EventArgs e)
    {

      fileList.Add(@"E:\\QLDownload\nba\\Action2.mp4");
      fileList.Add(@"E:\\QLDownload\nba\\Action3.mp4");
      fileList.Add(@"E:\\QLDownload\nba\\Action4.mp4");
      fileList.Add(@"E:\\QLDownload\nba\\Action5.mp4");
      for (int i = 0; i < fileList .Count ; i++)
      {
        listBox1.Items.Add(fileList [i]);
      }

      // Default selection of 1 Items 
      this.listBox1.SelectedIndex = 0;
      axWindowsMediaPlayer1 .URL = fileList [listBox1.SelectedIndex];
      axWindowsMediaPlayer1 .Ctlcontrols.play();
    }



 private void timer1_Tick(object sender, EventArgs e)
    {
      if (axWindowsMediaPlayer1 .playState == WMPLib.WMPPlayState.wmppsPlaying)
      {
        double d1 = Convert.ToDouble(axWindowsMediaPlayer1 .currentMedia.duration.ToString());
        double d2 = Convert.ToDouble(axWindowsMediaPlayer1 .Ctlcontrols.currentPosition.ToString()) + 1;
        if (d1 <= d2)
        {
          nextMusic(listBox1.SelectedIndex);
        }
      }
     }


 private void Form1_Load(object sender, EventArgs e)
    {
      axWindowsMediaPlayer1 .settings.autoStart = false ;
    
    }


void nextMusic(int index)
    {
      //listBox1.SelectedIndices.Clear();
      index++;
      if (index == listBox1.Items.Count)
      {
        index = 0;
      }
      axWindowsMediaPlayer1 .URL = fileList [index];
      listBox1.SelectedIndex = index;
      axWindowsMediaPlayer1 .Ctlcontrols.play();
    }

Reminder: Pay attention to the attributes of each component. If it can't run, adjust the attributes in time.

Due to requirements, you are not allowed to select files, so in the code, it is added by default, and listbox is hidden.

Problem: The following is to solve the path problem. If you package it, you must make it into a project path or a network path. Video files do not support built-in resources.


Related articles: