天天看點

c#制作自動播放的mp3

//添加播放音樂事件
        private void button4_Click(object sender, EventArgs e)
        {
            this.listBox1.Items.Clear(); //清空ListBox中的的是以資料
            DialogResult dr = folderBrowserDialog1.ShowDialog();
            if (dr == DialogResult.OK)
            {
                //指定目錄中的所選種的檔案夾
                DirectoryInfo  dir = Directory.CreateDirectory(folderBrowserDialog1.SelectedPath);
                GetAllFiles(dir);  //掃描使用者選種的檔案夾
            }
        }

        //掃描使用者選種的檔案夾
        private void GetAllFiles(DirectoryInfo dir)
        {           
            this.listBox1.Items.Clear();
            FileSystemInfo[] fileInfo =  dir.GetFileSystemInfos();  //傳回指定目錄檔案中的所有子檔案
            foreach (FileSystemInfo fileMp3 in fileInfo)
            {
                if (fileMp3 is DirectoryInfo)               
                    GetAllFiles((DirectoryInfo)fileMp3);                
                else
                {
                    string strPath = fileMp3.FullName; //擷取檔案的完整目錄
                    string strFullMp3 = (strPath.Substring(strPath.LastIndexOf(@"/") + 1)).ToString(); //擷取mp3的完整名稱
                    string forMart = strFullMp3.Substring(strFullMp3.Length - 3); //擷取檔案的格式

                    if (forMart == "mp3")
                    {
                        this.listBox1.Items.Add(strFullMp3); //将mp3的名字添加到ListBox中

                        //添加清單
                        wc = new WindowsMediaPlayerClass();
                        mc = wc.newMedia(strPath);
                        this.axWindowsMediaPlayer1.currentPlaylist.appendItem(mc);
                    }
                }
            }
        }

       
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true && mc!=null)
                axWindowsMediaPlayer1.Ctlcontrols.play();
            else if(checkBox1.Checked == false)
                axWindowsMediaPlayer1.Ctlcontrols.stop();
            else if (mc == null && checkBox1.Checked == true)
            {
                MessageBox.Show("請添加清單", "提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                checkBox1.Checked = false;
            }

        }