MP3播放器

暑假在家做了一个音乐播放器

主要功夫花在外观美化上,而不是代码优化,所以代码很渣,

拓展性也并不好,但却是我写的最大的一个作品,,

直接上代码

  1 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.IO;
 10 
 11 namespace MP3
 12 {
 13     public partial class Form1 : Form
 14     {
 15         public Form1()
 16         {
 17             InitializeComponent();
 18         }
 19         Point CPoint;
 20         PictureBox pictb = new PictureBox();
 21         bool play = false;
 22         bool change=true;
 23       //  string  fp;
 24         string filelist = Directory.GetCurrentDirectory() + "\\filelist.ini";
 25         string lovelist = Directory.GetCurrentDirectory()+"\\lovelist.ini";
 26        int m=0;
 27         private void Form1_Load(object sender, EventArgs e)
 28         {
 29             if (!File.Exists(filelist))
 30             {
 31                 FileStream fl = File.Create(filelist);
 32                 fl.Close();
 33             }
 34             if (!File.Exists(lovelist))
 35             {
 36                 FileStream ll = File.Create(lovelist);
 37                 ll.Close();
 38             }
 39             addfilelist(listViewall, filelist);
 40             addfilelist(listViewlove,lovelist);
 41             listswicth(0);
 42         }
 43         private void pictureBox_mousmove(object sender, MouseEventArgs e)
 44         {
 45             imageswicth(sender, Convert.ToInt16(((PictureBox)sender).Tag.ToString()), 0);
 46         }
 47         private void pictureBox_mouseleve(object sender, EventArgs e)
 48         {
 49             imageswicth(sender, Convert.ToInt16(((PictureBox)sender).Tag.ToString()), 1);
 50         }
 51         //拖动
 52         private void panel1_mousedown(object sender, MouseEventArgs e)
 53         {
 54             CPoint = new Point(-e.X, -e.Y);
 55         }
 56         private void panel1_mousemove(object sender, MouseEventArgs e)
 57         {
 58             if (e.Button == MouseButtons.Left)
 59             {
 60                 Point myposittion = Control.MousePosition;
 61                 myposittion.Offset(CPoint.X, CPoint.Y);
 62                 this.DesktopLocation = myposittion;
 63             }
 64         }
 65         //更改图片自定义方法
 66         public void imageswicth(object sender, int n, int ns)
 67         {  
 68             pictb = (PictureBox)sender;
 69             pictb.Image = null;
 70             switch (n)
 71             {
 72                 case 0:
 73                     {
 74                         if (ns == 0)
 75                             pictb.Image = Properties.Resources.最小化按钮;
 76                         if (ns == 1)
 77                             pictb.Image = Properties.Resources.最小化变色1;
 78                         break;
 79                     }
 80                 case 1:
 81                     {
 82                         if (ns == 0)
 83                             pictb.Image = Properties.Resources.关闭按钮1;
 84                         if (ns == 1)
 85                             pictb.Image = Properties.Resources.关闭变色1;
 86                         break;
 87                         
 88                     }
 89                 case 2:
 90                     {
 91                         if (ns == 0)
 92                             pictb.Image = Properties.Resources._9;
 93                         if (ns == 1)
 94                             pictb.Image = Properties.Resources._8;
 95                         break;
 96  
 97                     }
 98                 case 3:{
 99                             if (play == false)
100                             {
101                                 if (ns == 0)
102                                     pictb.Image = Properties.Resources._12;
103                                 if (ns == 1)
104                                     pictb.Image = Properties.Resources._6;
105                             }
106                             else
107                             {
108                                 if (ns == 0)
109                                     pictb.Image = Properties.Resources._5;
110                                 if (ns == 1)
111                                     pictb.Image = Properties.Resources._4;
112  
113                             }
114                         
115                         break;
116                         }
117                 case 4:
118                         {
119                             if (ns == 0)
120                                 pictb.Image = Properties.Resources._1;
121                             if (ns == 1)
122                                 pictb.Image = Properties.Resources._3;
123                             break;
124                         }
125                 case 5:
126                         {
127                             if (m == 0)
128                             {
129                                 pictb.Image = Properties.Resources.qiehuan122;
130                             }
131                             if (m == 1)
132                             {
133                                 if (ns == 0)
134                                     pictb.Image = Properties.Resources.qiehuanhoverbendi;
135                                 if (ns == 1&&m==1)
136                                 {
137                                     if(change)
138                                         pictb.Image = Properties.Resources.qiehuan1;
139                                     else
140                                         pictb.Image = Properties.Resources.qiehuan333;
141                                 }
142                                 if (ns == 1 && m == 0)
143                                 {
144                                     pictb.Image = Properties.Resources.qiehuan122;
145                                 }
146                             }
147                           
148                             break;
149                         }
150                 case 6:
151                         {
152                             if (m == 1)
153                             {
154                                 pictb.Image = Properties.Resources.qiehuan222;
155                                 break;
156                             }
157                              if (m==0)
158                             {
159                                 if (ns == 0)
160                                     pictb.Image = Properties.Resources.qiehuanhoverbofang;
161                                 if (ns == 1 && m == 0)
162                                 {
163                                     if(change)
164                                          pictb.Image = Properties.Resources.qiehuan2;
165                                     else
166                                         pictb.Image = Properties.Resources.qiehuan344;
167                                 }
168                                 if (ns == 1 && m ==1)
169                                     pictb.Image = Properties.Resources.qiehuan222;             
170                             }
171                            
172                             break;
173                         }
174              
175             }
176         }
177         //音乐列表切换自定义方法
178         public void listswicth(int n)
179         {
180             if (change)
181             {
182                 if (n == 0)
183                 {
184                     this.picbqiehuan2.Image = global::MP3.Properties.Resources.qiehuan2;
185                     this.pictbqiehuan1.Image = global::MP3.Properties.Resources.qiehuan122;
186                     listViewlove.Hide();
187                     listViewall.Show();
188                 }
189                 if (n == 1)
190                 {
191                     this.picbqiehuan2.Image = global::MP3.Properties.Resources.qiehuan222;
192                     this.pictbqiehuan1.Image = global::MP3.Properties.Resources.qiehuan1;
193                     listViewlove.Show();
194                     listViewall.Hide();
195                 }
196               
197             }
198             else
199             {
200                 if (n == 0)
201                 {
202                     this.picbqiehuan2.Image = global::MP3.Properties.Resources.qiehuan344;
203                     this.pictbqiehuan1.Image = global::MP3.Properties.Resources.qiehuan122;
204                     listViewlove.Hide();
205                     listViewall.Show();
206                 }
207                 if (n == 1)
208                 {
209                     this.picbqiehuan2.Image = global::MP3.Properties.Resources.qiehuan222;
210                     this.pictbqiehuan1.Image = global::MP3.Properties.Resources.qiehuan333;
211                     listViewlove.Show();
212                     listViewall.Hide();
213                 }
214               
215             }
216            
217         }
218         //添加到播放列表自定义方法
219         private void addplaylist()
220         {
221             string[] items = new string[2];
222             items[0] = axWindowsMediaPlayer1.currentMedia.name;
223             items[1] = axWindowsMediaPlayer1.currentMedia.sourceURL;
224             ListViewItem lvi = new ListViewItem(items);
225             int count=0;
226             listViewlove.BeginUpdate();
227             for (int j = 0; j < listViewlove.Items.Count; j++)
228                 if (axWindowsMediaPlayer1.currentMedia.name == listViewlove.Items[j].SubItems[0].Text)
229                     count++;
230            //判断是否有相等
231             if (count == 0)
232             {
233                 //排序
234                 listViewlove.Items.Add(lvi);
235                 for (int j = listViewlove.Items.Count-1; j >0; j--)
236                 {
237                     listViewlove.Items[j].SubItems[0].Text = listViewlove.Items[j - 1].SubItems[0].Text;
238                     listViewlove.Items[j].SubItems[1].Text = listViewlove.Items[j - 1].SubItems[1].Text;
239                 }
240                 listViewlove.Items[0].SubItems[0].Text = axWindowsMediaPlayer1.currentMedia.name;
241                 listViewlove.Items[0].SubItems[1].Text = axWindowsMediaPlayer1.currentMedia.sourceURL;
242             }
243             listViewlove.EndUpdate();   
244         }
245         //歌曲切换自定义方法
246         private void nextmusic(int n)
247         {
248             //上一首
249             if (n == 0)
250             {
251                 string s = label1.Text;
252                 for (int i = 0; i < listViewall.Items.Count - 1; i++)
253                 {
254                     if (listViewall.Items[i].SubItems[0].Text == s)
255                     {
256                         axWindowsMediaPlayer1.URL = listViewall.Items[i - 1].SubItems[1].Text;
257                         label1.Text = axWindowsMediaPlayer1.currentMedia.getItemInfo("Title");
258                         addplaylist();
259                         break;
260                     }
261                 }
262             }
263             //下一首
264             if (n == 1)
265             {
266                 string s = label1.Text;
267                 for (int i = 0; i < listViewall.Items.Count - 1; i++)
268                 {
269                     if (listViewall.Items[i].SubItems[0].Text == s)
270                     {
271                         axWindowsMediaPlayer1.URL = listViewall.Items[i + 1].SubItems[1].Text;     
272                         label1.Text = axWindowsMediaPlayer1.currentMedia.getItemInfo("Title");
273                         addplaylist();
274                         break;
275                     }
276                 }
277             }
278             //随机播放
279             if (n == 2)
280             {
281                 Random r =new Random();
282                 int i = r.Next(listViewall.Items.Count);
283                 axWindowsMediaPlayer1.URL = listViewall.Items[i].SubItems[1].Text;
284                         label1.Text = axWindowsMediaPlayer1.currentMedia.getItemInfo("Title");
285                         addplaylist();
286             
287             }
288 
289         }
290         //列表切换按钮
291         private void pictbqiehuan1_Click(object sender, EventArgs e)
292         {
293             m = 0;
294             listswicth(0);
295         }
296         private void picbqiehuan2_Click(object sender, EventArgs e)
297         {
298             m = 1;
299             listswicth(1);
300         }
301         //关闭按钮
302         private void pictureBox1_Click(object sender, EventArgs e)
303         {
304             StreamWriter sw = new StreamWriter(lovelist);
305             for (int i = 0; i < listViewlove.Items.Count; i++)
306                 sw.WriteLine(listViewlove.Items[i].SubItems[1].Text);
307             sw.Close();
308                 this.Close();
309         }
310         //最小化按钮
311         private void pictureBox2_Click(object sender, EventArgs e)
312         {
313             this.WindowState = FormWindowState.Minimized;
314         }
315         //播放按钮
316         private void picbstart_Click(object sender, EventArgs e)
317         {
318             if (play == false)
319             {
320                 if (label1.Text == string.Empty && openFileDialog1.ShowDialog() == DialogResult.OK)
321                 {
322                     listViewall.Items.Add(openFileDialog1.FileName);
323                     axWindowsMediaPlayer1.URL = openFileDialog1.FileName;
324                    label1.Text = axWindowsMediaPlayer1.currentMedia.getItemInfo("Title");
325                     play = true;
326                 }
327          
328                 if (label1.Text != string.Empty)
329                 {
330                     axWindowsMediaPlayer1.Ctlcontrols.play();
331                     play = true;
332                 } 
333             }
334             else
335             {
336                 play = false;
337                 axWindowsMediaPlayer1.Ctlcontrols.pause();
338             }       
339         }
340         //声音
341         private void hScrollBar2_Scroll(object sender, ScrollEventArgs e)
342         {
343             axWindowsMediaPlayer1.settings.volume = e.NewValue;
344         }
345         //进度
346         private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
347         {
348            axWindowsMediaPlayer1.Ctlcontrols.currentPosition = e.NewValue;
349         }
350         private void timer1_Tick(object sender, EventArgs e)
351         {   
352             if (play == true)
353             {
354                 hScrollBar1.Maximum = (int)axWindowsMediaPlayer1.currentMedia.duration;
355                 hScrollBar1.Minimum = 0;
356                 hScrollBar1.Value = (int)axWindowsMediaPlayer1.Ctlcontrols.currentPosition;
357                 hScrollBar2.Value = axWindowsMediaPlayer1.settings.volume;
358                 label3.Text = axWindowsMediaPlayer1.currentMedia.durationString;
359                 label2.Text = axWindowsMediaPlayer1.Ctlcontrols.currentPositionString;
360                 if (axWindowsMediaPlayer1.playState ==WMPLib.WMPPlayState.wmppsStopped)
361                     nextmusic(1);
362             }
363            
364         }
365         //添加到列表自定义方法
366         private void addfilelist(object sender,string list)
367         {
368                 ListViewItem lvi;
369                 ListView lv=(ListView)sender;
370                 StreamReader sr = new StreamReader(list);     
371                 string[] items = new string[2];
372                 lv.BeginUpdate();
373                while(sr.Peek()>0)
374                 {
375                     string item1 = sr.ReadLine();
376                     items[1] = item1;                                //完整路径
377                     items[0] = Path.GetFileNameWithoutExtension(item1);  //文件名
378                     lvi = new ListViewItem(items);
379                     lv.Items.Add(lvi);
380                 }
381                 lv.EndUpdate();
382                 sr.Close();
383 
384         }
385         //打开窗口添加文件到列表文件
386         public void addfile()
387         {
388             if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
389             {
390                 string fp = folderBrowserDialog1.SelectedPath;
391                 StreamWriter sw = new StreamWriter(filelist);
392                 string item1;
393                 string[] files = Directory.GetFiles(fp);
394                 for (int i = 0; i < files.Length; i++)
395                 {
396                     string ext = (Path.GetExtension(files[i])).ToLower();
397                     if (ext == ".mp3")
398                     {
399                         item1 = Path.GetFullPath(files[i]);
400                         sw.WriteLine(item1);
401                     }
402                 }
403                 sw.Close();
404             }
405         }
406         // 双击事件 暂停,播放
407         private void playitem(object sender, EventArgs e)
408         {
409             if (listViewall.SelectedItems.Count > 0)
410             {
411                 ListViewItem lvi = listViewall.SelectedItems[0];
412                 axWindowsMediaPlayer1.URL =lvi.SubItems[1].Text;
413                 addplaylist();
414                 picbstart.Image = Properties.Resources._4;
415                 label1.Text = axWindowsMediaPlayer1.currentMedia.getItemInfo("Title");
416                 play = true; 
417             }
418         }
419         private void loveplayitem(object sender, EventArgs e)
420         {
421             if (listViewlove.SelectedItems.Count > 0)
422             {
423                 ListViewItem lvi = listViewlove.SelectedItems[0];
424                 axWindowsMediaPlayer1.URL = lvi.SubItems[1].Text;
425                 picbstart.Image = Properties.Resources._4;
426                 label1.Text = axWindowsMediaPlayer1.currentMedia.getItemInfo("Title");
427                 play = true;
428             }
429         }
430         //下一首
431         private void pictureBox5_Click(object sender, EventArgs e)
432         {
433             nextmusic(1);
434         }
435         //上一首
436         private void pictureBox4_Click(object sender, EventArgs e)
437         {
438             nextmusic(0);
439         }
440         //皮肤切换
441         private void button1_Click(object sender, EventArgs e)
442         {
443             if (change)
444             {
445                 this.BackColor = System.Drawing.Color.SlateGray;
446                 this.panel1.BackgroundImage = global::MP3.Properties.Resources.from31;
447                 this.panellow.BackgroundImage = global::MP3.Properties.Resources.fromlow2;
448                 change = !change;
449                 listswicth(0);
450             }
451             else
452             {
453 
454                 this.BackColor = System.Drawing.Color.Green;
455                 this.panel1.BackgroundImage = global::MP3.Properties.Resources.panneltitie;
456                 this.panellow.BackgroundImage = global::MP3.Properties.Resources.pannellowgree;
457                 change = !change;
458                 listswicth(0);
459             }
460         }
461 
462         private void labadd_Click(object sender, EventArgs e)
463         {
464             addfile();
465             addfilelist(listViewall, filelist);
466         }
467 
468     }
469 }
Code

 

posted @ 2013-09-22 23:44  btohad  阅读(286)  评论(0编辑  收藏  举报