switch (type) { case 0: currentPlay ++; if(currentPlay > numOfMusic)return 0; else return currentPlay; case 1: currentPlay ++; if(currentPlay > numOfMusic) return 1; else return currentPlay; case 2: return currentPlay; case 3: Random rdm = new Random(unchecked((int)DateTime.Now.Ticks)); currentPlay = rdm.Next() % numOfMusic; if(currentPlay == 0) return numOfMusic; else return currentPlay; default: return 0; } } } Player类中包括一个windowsMediaPlayer对象myPlayer,一个存储播放列表的数组playlist,记录歌曲总数的numOfMusic,以及当前播放的歌曲对应列表中的序号currentplay; 另外有四个方法分别是Play,AddFile,DelFile,以及获得下次播放序号的NextPlay
分功能列出其他主要代码
添加单个歌曲
if(this.openFileDialog1.ShowDialog() == DialogResult.OK) { string path = this.openFileDialog1.FileName; FileInfo f = new FileInfo(path); MyPlayer.AddFile(f.FullName); string STRFILE = Convert.ToString(MyPlayer.NumOfMusic); for(int i = 1;i<=5-STRFILE.Length;i++)STRFILE+=’ ’; STRFILE += f.Name; this.listBox1.Items.Add(STRFILE); } 添加一个文件夹及其所有子文件夹的歌曲
利用递归函数showfiles实现所有层歌曲都添加到歌曲列表中。
private void showfiles(string path,ListBox listBox1) { DirectoryInfo dir = new DirectoryInfo(path); foreach(FileInfo f in dir.GetFiles("*.mp3")) { MyPlayer.AddFile(f.FullName); } foreach(DirectoryInfo f in dir.GetDirectories()) { showfiles(f.FullName,listBox1); } 删除和清空直接调用类Player中的AddFile和DelFile函数