c#winform循環播放多個視頻


    環境: vs2015 +winform

    首先,vs自帶組件很方便,所以,用windowMediaplayer組件,如果做單曲循環播放的話,加個屬性:

             axWindowsMediaPlayer1.settings.autoStart = true;            //設置自動播放
             axWindowsMediaPlayer1.settings.setMode("loop", true);     //設置循環播放

   言歸正傳:

   一:拖入組件button  ,windowMediaplayer,listbox,timer

  二:

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

            //默認選擇第一項
            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();
        }

 

    提醒: 注意各個組件的自身屬性,運行不了,及時調屬性, .

    由於需求原因,不讓選擇文件,所以在代碼里,默認添加的, 並把listbox隱藏了.

 

問題: 下面就要解決路徑問題了.如果打包,必須弄成項目路徑或者網絡路徑, 視頻文件並不支持內置資源.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM