C#簡單音樂播放器ListBox歌單列表

來源:互聯網
上載者:User

標籤:

     簡單的音樂播放器實現其實並不難,直接用axWindowsmediaplayer來做,這裡沒有什麼技術含量的,但是axWindowsmediaplayer是不會產生歌單列表的,也就是說,在每次添加音樂後axWindowsmediaplayer是沒有記錄的,那麼我就來簡單的做下歌單列表。

     axWindowsmediaplayer的工作原理其實就是根據Path來載入的,在寫歌單時我們需要先來做兩件事,1、儲存歌曲Path 2、儲存歌曲名字 這裡暫且不考慮資料庫,我使用IO操作通過txt檔案來儲存這兩種資訊。

     思路過程:

              判斷檔案是否存在  -----存在 ------ 寫入流存入資料 ------更新ListBox

                                      -----不存在 ------建立兩個檔案 ------- 寫入流存入資料 ------更新ListBox

              這都不是問題,重點在我們必須使用兩個ListBox,一個顯示歌名,一個存入路徑,並且要使兩個ListBox相關聯

      IO作業碼:

 1  public void IOStreamName() 2         {  3             if (File.Exists(NamePath)) 4             { 5                 // 存入檔案名稱 6                // MessageBox.Show("1存在!"); 7                 StreamWriter NameWriter = new StreamWriter(NamePath,true); 8                 if (ListSafeFileName.Items.Count > 0) 9                 {10                     for (int i = 0; i < ListSafeFileName.Items.Count; i++)11                     {12                         NameWriter.WriteLine(ListSafeFileName.Items [ListSafeFileName .Items .Count -1].ToString () );13                         break;14                     }15                     NameWriter.Close();16                 } 17             }18             else19             {20                 // 建立檔案名稱檔案21                 FileStream SafeFileName = new FileStream(NamePath, FileMode.Create);22                 SafeFileName.Close();23                 StreamWriter NameWriter = new StreamWriter(NamePath);24                 for (int i = 0; i < ListSafeFileName.Items.Count; i++)25                 {26                     NameWriter.WriteLine(ListSafeFileName.Items[ListSafeFileName.Items.Count - 1].ToString());27                 }28                 NameWriter.Close();29                 30             }31         }
IOStreamName
 1 public void IOStreamFileName() 2         { 3             if (File.Exists(FileNamePath)) 4             { 5                 // 存入檔案路徑 6                // MessageBox.Show("2存在!"); 7                 StreamWriter FileNameWriter = new StreamWriter(FileNamePath, true); 8                 if (ListFileName.Items.Count > 0) 9                 {10                     for (int i = 0; i < ListFileName.Items.Count; i++)11                     {12                         FileNameWriter.WriteLine(ListFileName.Items[ListFileName.Items.Count - 1].ToString());13                         break;14                     }15                     FileNameWriter.Close();16                 }17             }18             else19             {20                 // 建立路徑檔案21                 FileStream FileName = new FileStream(FileNamePath, FileMode.Create);22                 FileName.Close();23                 StreamWriter FileNameWriter = new StreamWriter(FileNamePath);24                 for (int i = 0; i < ListFileName.Items.Count; i++)25                 {26                     FileNameWriter.WriteLine(ListFileName.Items[ListFileName.Items.Count - 1].ToString());27                 }28                 FileNameWriter.Close();29                30             }31         }
IOStreamFileName

    ListBox關聯思路

1  private void ListSafeFileName_MouseDoubleClick(object sender, MouseEventArgs e)2         {3             if (ListSafeFileName.SelectedIndex >= 0)4             {5                 ListFileName.SelectedIndex = ListSafeFileName.SelectedIndex;6                 WinPlayer.URL = ListFileName.SelectedItem.ToString();7             }8         }
ListSafeFileName

    啟動即更新列表 (這裡要說下,更新,我寫了兩個函數,這裡的演算法是比較經典的,是真的把ListBox玩出了花,代碼寫到這其實是最過癮的)

 1  public void IOStreamReadName() 2         { 3             if (File.Exists(NamePath)) 4             { 5                 StreamReader MusicName = new StreamReader(NamePath); 6                 string[] TxtData = File.ReadAllLines(NamePath); 7                 for (int i = 0; i < TxtData.Length; i++) 8                 { 9                     ListSafeFileName.Items.Add(MusicName.ReadLine());10                 }11                 MusicName.Close();12             }13 14         }
IOStreamReadName
 1 public void IOStreamReadFileName() 2         { 3             if (File.Exists(FileNamePath)) 4             { 5                 StreamReader MusicFileName = new StreamReader(FileNamePath); 6                 string[] TxtData = File.ReadAllLines(FileNamePath); 7                 for (int i = 0; i < TxtData.Length; i++) 8                 { 9                     ListFileName.Items.Add(MusicFileName.ReadLine());10                 }11                 MusicFileName.Close();12             }13         }
IOStreamReadFileName

 

       這裡貼出來的都是核心代碼了,其實後來想想txt檔案是不便於管理的,用泛型集合也挺好,有待改進吧。

C#簡單音樂播放器ListBox歌單列表

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.