利用Window Media Player 控件自己做一款小巧的mp播放器来听音乐 是不是很享受呢?今天刚写出来的听听mp感觉还不错哦 闲话少说进入正题
Mp播放器主要完成下列功能:
添加歌曲可以添加单个乐曲或者指定文件夹内包括其子文件夹内的所有mp乐曲到播放列表
删除指定歌曲或所有歌曲
播放的控制包括选择上一首下一首播放顺序播放循环播放和随机播放循环播放又分单个歌曲的循环播放和所有歌曲的循环播放
首先建立类player
public class Player
{
private WMPLibWindowsMediaPlayer myPlayer;
private string[] playList;
private int numOfMusic;
private int currentPlay;
public int NumOfMusic
{
get
{
return numOfMusic;
}
}
public WMPLibWMPPlayState playstate
{
get
{
return myPlayerplayState;
}
}
public string PlayList(int num)
{
return playList[num];
}
public Player(AxWMPLibAxWindowsMediaPlayer mediaPlayer)
{
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 ; i++)
{
playList[i] = playList[i + ];
}
numOfMusic ;
}
public void play(int selectNum)
{
myPlayerURL = playList[selectNum];
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)DateTimeNowTicks));
currentPlay = rdmNext() % numOfMusic;
if(currentPlay == ) return numOfMusic;
else return currentPlay;
default:
return ;
}
}
}
Player类中包括一个windowsMediaPlayer对象myPlayer一个存储播放列表的数组playlist记录歌曲总数的 numOfMusic以及当前播放的歌曲对应列表中的序号currentplay;另外有四个方法分别是PlayAddFileDelFile以及获得下次播放序号的NextPlay
分功能列出其他主要代码
添加单个歌曲
if(thisopenFileDialogShowDialog() == DialogResultOK)
{
string path = thisopenFileDialogFileName;
FileInfo f = new FileInfo(path);
MyPlayerAddFile(fFullName);
string STRFILE = ConvertToString(MyPlayerNumOfMusic);
for(int i = ;i<=STRFILELength;i++)STRFILE+= ;
STRFILE += fName;
thislistBoxItemsAdd(STRFILE);
}
添加一个文件夹及其所有子文件夹的歌曲
利用递归函数showfiles实现所有层歌曲都添加到歌曲列表中
private void showfiles(string pathListBox listBox)
{
DirectoryInfo dir = new DirectoryInfo(path);
foreach(FileInfo f in dirGetFiles(*mp))
{
MyPlayerAddFile(fFullName);
}
foreach(DirectoryInfo f in dirGetDirectories())
{
showfiles(fFullNamelistBox);
}
删除和清空直接调用类Player中的AddFile和DelFile函数
实现播放上一首
if(listBoxSelectedIndex >= )
{
listBoxSelectedIndex ;
if(listBoxSelectedIndex <)listBoxSelectedIndex = MyPlayerNumOfMusic ;
MyPlayerplay(listBoxSelectedIndex + );
}
下一首
if(listBoxSelectedIndex >= )
{
listBoxSelectedIndex = (listBoxSelectedIndex + ) % MyPlayerNumOfMusic;
MyPlayerplay(listBoxSelectedIndex + );
}
播放的控制
利用Player的NextPlay方法返回的值来选择下一次播放的内容
同时利用PlayStateChange事件来实现由一曲到下一曲的替换但是在响应PlayStateChange事件的时候直接改变Player的url无法让它直接播放下一曲解决方法如下:
private void axWindowsMediaPlayer_PlayStateChange(object sender AxWMPLib_WMPOCXEvents_PlayStateChangeEvent e)
{
if(MyPlayerplaystate == WMPLibWMPPlayStatewmppsMediaEnded)
{
timerStart();
}
}
private void timer_Tick(object sender SystemEventArgs e)
{
timerStop();
int selectnum = ;
if(menuItemChecked)selectnum = MyPlayerNextPlay();
else if (menuItemChecked)selectnum = MyPlayerNextPlay();
else if (menuItemChecked)selectnum = MyPlayerNextPlay();
else if (menuItemChecked)selectnum = MyPlayerNextPlay();
if(selectnum != )
{
listBoxSelectedIndex = selectnum ;
MyPlayerplay(selectnum);
}
}
满足一首歌曲结束的条件的时候唤醒计时器计时器ms内就响应函数timer_Tick在这个函数里实现下一首歌曲的选择播放便可以顺利进行