//添加播放音乐事件
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;
}
}