利用mediaplayer打造自己的mp3播放器

利用mediaplayer打造自己的mp3播放器<script language="javascript" type="text/javascript"> document.title="利用mediaplayer打造自己的mp3播放器 - "+document.title </script>


           

         利用Window Media Player 控件自己做一款小巧的mp3播放器來聽音樂 ,是不是很享受呢?今天剛寫出來的,聽聽mp3感覺還不錯哦。 閒話少說,進入正題。

Mp3播放器主要完成下列功能:

1.  添加歌曲,可以添加單個樂曲或者指定文件夾內包括其子文件夾內的所有mp3樂曲到播放列表。

2.  刪除指定歌曲或所有歌曲。

3.   播放的控制。包括選擇上一首,下一首播放,順序播放,循環播放和隨機播放。循環播放又分單個歌曲的循環播放和所有歌曲的循環播放。

首先建立類player

public class Player

     {

         private AxWMPLib.AxWindowsMediaPlayer myPlayer;

         private string[] playList;

         private int numOfMusic;

         private int currentPlay;

         public int NumOfMusic

         {

              get

              {

                   return numOfMusic;

              }

         }

         public WMPLib.WMPPlayState playstate

         {

              get

              {

                   return myPlayer.playState;

              }

         }

         public string PlayList(int num)

         {

              return playList[num];

         }

         public Player(AxWMPLib.AxWindowsMediaPlayer mediaPlayer)

         {

              myPlayer = mediaPlayer;

              playList = new string[1000];

              numOfMusic = 0;

         }

         public void AddFile(string path)

         {

              if(numOfMusic < 1000)

              {

                   numOfMusic ++;

                   playList[numOfMusic] = path;

              }

         }

         public void DelFile(int selectNum)

         {

              for(int i = selectNum; i <= numOfMusic - 1; i++)

              {

                   playList[i] = playList[i + 1];

              }

              numOfMusic --;

         }

         public void play(int selectNum)

         {

              myPlayer.URL = playList[selectNum];

              currentPlay = selectNum;

         }

         public int NextPlay(int type)

         {

              /* type = 0 順序

                 type = 1 重複播放全部

                 type = 2 重複播放一首

                 type = 3 隨機播放

              */

              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中的AddFileDelFile函數

    

     實現播放上一首

         if(listBox1.SelectedIndex >= 0)

          {

               listBox1.SelectedIndex --;

               if(listBox1.SelectedIndex <0)listBox1.SelectedIndex = MyPlayer.NumOfMusic - 1;

               MyPlayer.play(listBox1.SelectedIndex + 1);

          }

     下一首

if(listBox1.SelectedIndex >= 0)

          {

              listBox1.SelectedIndex = (listBox1.SelectedIndex + 1) % MyPlayer.NumOfMusic;

              MyPlayer.play(listBox1.SelectedIndex + 1);

          }

     播放的控制

         利用PlayerNextPlay方法返回的值來選擇下一次播放的內容。

同時利用PlayStateChange事件來實現由一曲到下一曲的替換,但是在響應PlayStateChange事件的時候直接改變Playerurl無法讓它直接播放下一曲,解決方法如下:

private void axWindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)

          {

              if(MyPlayer.playstate == WMPLib.WMPPlayState.wmppsMediaEnded)

              {

                   timer1.Start();

              }

         }

 

 

         private void timer1_Tick(object sender, System.EventArgs e)

         {

              timer1.Stop();

              int selectnum = 0;

              if(menuItem13.Checked)selectnum = MyPlayer.NextPlay(0);

              else if (menuItem15.Checked)selectnum = MyPlayer.NextPlay(1);

              else if (menuItem16.Checked)selectnum = MyPlayer.NextPlay(2);

              else if (menuItem17.Checked)selectnum = MyPlayer.NextPlay(3);

              if(selectnum != 0)

              {

                   listBox1.SelectedIndex = selectnum - 1;

                   MyPlayer.play(selectnum);

              }

}

滿足一首歌曲結束的條件的時候喚醒計時器,計時器100ms內就響應函數timer1_Tick,在這個函數裏實現下一首歌曲的選擇播放便可以順利進行.

至此主要功能便完成了!立刻用來聽聽mp3,自己的東西感覺就是不一樣哦!

發佈了36 篇原創文章 · 獲贊 0 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章