利用Window Media Player 控件自己做一款小巧的mp
Mp
首先建立類player
public class Player
{
private WMPLib
private string[] playList;
private int numOfMusic;
private int currentPlay;
public int NumOfMusic
{
get
{
return numOfMusic;
}
}
public WMPLib
{
get
{
return myPlayer
}
}
public string PlayList(int num)
{
return playList[num];
}
public Player(AxWMPLib
{
myPlayer = mediaPlayer;
playList = new string[
numOfMusic =
}
public void AddFile(string path)
{
if(numOfMusic <
{
numOfMusic ++;
playList[numOfMusic] = path;
}
}
public void DelFile(int selectNum)
{
for(int i = selectNum; i <= numOfMusic
{
playList[i] = playList[i +
}
numOfMusic
}
public void play(int selectNum)
{
myPlayer
currentPlay = selectNum;
}
public int NextPlay(int type)
{
/* type =
type =
type =
type =
*/
switch (type)
{
case
currentPlay ++;
if(currentPlay > numOfMusic)return
else return currentPlay;
case
currentPlay ++;
if(currentPlay > numOfMusic) return
else return currentPlay;
case
return currentPlay;
case
Random rdm = new Random(unchecked((int)DateTime
currentPlay = rdm
if(currentPlay ==
else return currentPlay;
default:
return
}
}
}
Player類中包括一個windowsMediaPlayer對象myPlayer
分功能列出其他主要代碼
添加單個歌曲
if(this
{
string path = this
FileInfo f = new FileInfo(path);
MyPlayer
string STRFILE = Convert
for(int i =
STRFILE += f
this
}
添加一個文件夾及其所有子文件夾的歌曲
利用遞歸函數showfiles實現所有層歌曲都添加到歌曲列表中
private void showfiles(string path
{
DirectoryInfo dir = new DirectoryInfo(path);
foreach(FileInfo f in dir
{
MyPlayer
}
foreach(DirectoryInfo f in dir
{
showfiles(f
}
刪除和清空直接調用類Player中的AddFile和DelFile函數
實現播放上一首
if(listBox
{
listBox
if(listBox
MyPlayer
}
下一首
if(listBox
{
listBox
MyPlayer
}
播放的控制
利用Player的NextPlay方法返回的值來選擇下一次播放的內容
同時利用PlayStateChange事件來實現由一曲到下一曲的替換
private void axWindowsMediaPlayer
{
if(MyPlayer
{
timer
}
}
private void timer
{
timer
int selectnum =
if(menuItem
else if (menuItem
else if (menuItem
else if (menuItem
if(selectnum !=
{
listBox
MyPlayer
}
}
滿足一首歌曲結束的條件的時候喚醒計時器
From:http://tw.wingwit.com/Article/program/net/201311/15463.html