播放器01:保存文件到数组, 添加文件到Listbox,去路径,不加重复文件

初学C#记录历程,记录心情。

1. 添加Windows Media Player COM 组件

右击 Toolbox-->Choose Items-->COM Components, 选择Windows Media Player, OK, 这时会看到Toolbox中出现了Windows Media Player控件,添加到窗口就可以了,

名字:mediaPlayer

2. 添加Listbox ,lstPlayer

3. 添加MenuStrip,  addFiles

4.添加 OpenFileDialog 控件,openFileDialog

 

View Code
 1                                                  protected List<string> playList = new List<string>();  //定义一个list来保存加进到listbox里面的文件
 2                   
 3                                
 4                                                 //去掉文件路径,只留文件名字
 5                                                 public string DeletePath(string strName)
 6                                                 {
 7                                                    string afterDeletePath = "";
 8                                                    int lastIndex=0;
 9                                                    lastIndex= strName.LastIndexOf("\\") + 1;        //获取\ 在tempFileName中最后一次出现的索引位置
10                                                    afterDeletePath = strName.Substring(lastIndex, strName.Length - lastIndex);//去掉文件路径,只取文件名字(取子字符串)
11                                                    return afterDeletePath;
12                                                  }
13 
14                                                 private void SaveFiles()
15                                                 {
16                                   
17                                                         foreach (var fileName in openFileDialog.FileNames)
18                                                         {
19                                                             if (playList.Contains(fileName) == true)          //不加重复的   
20                                                             {
21                                                                 continue;
22                                                             }
23                                                             else
24                                                             {
25                                                                 playList.Add(fileName);                         //保存原始文件到playList
26                                                                 lstPlayer.Items.Add(DeletePath(fileName));      //添加文件到listbox
27                                                       
28                                                             }
29                                                         }
30                                                     }
31                                  
32                                               
33                                                 //设置正在播放的歌曲为选中状态
34                                                 private void PlaySelected()
35                                                 {
36                                                     int i=0;
37                                                     for (i = 0; i < lstPlayer.Items.Count; i++)
38                                                     {
39                                                         if (mediaPlayer.URL == playList[i])
40                                                         {
41                                                             lstPlayer.SetSelected(i,true);
42                                                         }
43                                                     }
44 
45                                                 }
46 
47     
48 
49                                           public myAudioPlayer()
50                                                 {
51                                                     InitializeComponent();
52                                                     mediaPlayer.uiMode = "Full";
53                                                 }
54 
55                                                 private void addFilesToolStripMenuItem_Click(object sender, EventArgs e)
56                                                 {
57                                                     openFileDialog.Multiselect = true;  //可以选择多个文件
58 
59                                                     openFileDialog.Filter = "mp3文件|*.mp3|wma文件|*.wma";   //只显示.mp3和 .wma格式的文件
60 
61 
62                                                     if (this.openFileDialog.ShowDialog() == DialogResult.OK)    //判断是否单击确定按钮
63                                                     {
64                                                         try
65                                                         {
66                                          
67                                                            SaveFiles();
68 
69                                                         }
70                                                         catch (Exception ex)
71                                                         {
72                                                             MessageBox.Show("unable to load file\n\n" + ex.Message);
73                                                         }
74 
75                                                     }
76                                                     mediaPlayer.URL = openFileDialog.FileNames[0];     //默认播放刚加进到列表里面的文件
77                                                     PlaySelected();
78                                              }
79 
80                                           
81                                              
82                                         
原文地址:https://www.cnblogs.com/bloomalone/p/2823201.html