播放器03:以文件夹的形式添加整个文件夹里面的文件到播放列表,播放刚加进来的第一首歌曲,默认顺序播放...
初学C#记录历程,记录心情。
添加FolderBrowserDialog: folderBrowserDialog, 属性RootFolder 为Desktop,ShowNewFolderButton 为True
UI:
View Code1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 using System.Threading; 10 using System.IO; 11 12 namespace OrderPlay 13 { 14 public partial class myAudioPlayer : Form 15 { 16 17 private bool close = false;// 关闭窗口标志位 18 private bool running = false;//线程结束标志位 19 protected List<string> playList = new List<string>(); //定义一个list来保存加进到listbox里面的文件 20 21 22 //去掉文件路径,只留文件名字 23 public string DeletePath(string strName) 24 { 25 string afterDeletePath = ""; 26 int lastIndex=0; 27 lastIndex= strName.LastIndexOf("\") + 1; //获取 在tempFileName中最后一次出现的索引位置 28 afterDeletePath = strName.Substring(lastIndex, strName.Length - lastIndex);//去掉文件路径,只取文件名字(取子字符串) 29 return afterDeletePath; 30 } 31 32 33 34 private void SaveFilesFolder() 35 { 36 string[] folderName = Directory.GetFiles(this.folderBrowserDialog.SelectedPath); 37 foreach (string f in folderName) 38 { 39 40 if (playList.Contains(f) == true) //不加重复的 41 { 42 continue; 43 } 44 else 45 { 46 playList.Add(f); 47 lstPlayer.Items.Add(DeletePath(f)); 48 49 } 50 51 } 52 53 } 54 //设置正在播放的歌曲为选中状态 55 private void PlaySelected() 56 { 57 int i=0; 58 for (i = 0; i < lstPlayer.Items.Count; i++) 59 { 60 if (mediaPlayer.URL == playList[i]) 61 { 62 lstPlayer.SetSelected(i,true); 63 } 64 } 65 66 } 67 68 //顺序播放 69 private void OrderPlay() 70 { 71 if (!running) 72 { 73 // 启动另一线程检测 mediaPlayer 的播放状态,循环播放列表里的歌曲 74 running = true; 75 Thread thread = new Thread(new ThreadStart(this.CheckStatus)); 76 thread.IsBackground = false; //设置后台线程为false 77 thread.Start(); 78 } 79 } 80 81 82 //检查线程状态,顺序播放 83 private void CheckStatus() 84 { 85 while (running && !close) 86 { 87 try 88 { 89 if (mediaPlayer.playState == WMPLib.WMPPlayState.wmppsStopped && lstPlayer.Items.Count > 0) //是否播放停止且列表有文件 90 { 91 if (lstPlayer.InvokeRequired) //是否跨线程 92 { 93 lstPlayer.BeginInvoke(new MethodInvoker(() =>//BeginInvoke方法可以使用线程异步地执行委托所指向的方法 94 { 95 SetSelectedIndex(); 96 97 }), null); 98 } 99 else 100 { 101 SetSelectedIndex(); 102 103 } 104 105 } 106 107 } 108 catch (Exception ex) 109 { 110 MessageBox.Show("出错" + ex.ToString()); 111 } 112 System.Threading.Thread.Sleep(1000);//状态检测延时1秒,加快打开歌曲的速度 113 } 114 running = false; 115 116 117 } 118 119 //判断是否到最后一个,否则下移 120 private void SetSelectedIndex() 121 { 122 123 if (lstPlayer.SelectedIndex + 1 >= lstPlayer.Items.Count) //是否到最下面 124 { 125 lstPlayer.SelectedIndex = 0; //回到第一个 126 } 127 else 128 { 129 lstPlayer.SelectedIndex = lstPlayer.SelectedIndex + 1; //下移 130 } 131 132 mediaPlayer.URL = playList[lstPlayer.SelectedIndex]; //播放 133 134 } 135 136 public myAudioPlayer() 137 { 138 InitializeComponent(); 139 mediaPlayer.uiMode = "Full"; 140 } 141 142 143 private void addFolderToolStripMenuItem_Click(object sender, EventArgs e) 144 { 145 DialogResult result=this.folderBrowserDialog.ShowDialog(); 146 string[] folderName = Directory.GetFiles(this.folderBrowserDialog.SelectedPath); 147 148 if(result==DialogResult.OK) 149 { 150 try 151 { 152 SaveFilesFolder(); 153 mediaPlayer.URL = folderName[0]; 154 PlaySelected(); 155 } 156 catch (Exception ex) 157 { 158 MessageBox.Show("unable to load file " + ex.Message); 159 } 160 } 161 162 OrderPlay(); //默认顺序播放 163 164 } 165 166 } 167 }