利用Window Media Player 控件自己做一款小巧的MP3播放器來聽音樂 ,是不是很享受呢?今天剛寫出來的,聽聽mp3感覺還不錯哦。 閑話少說,進入正題。
Mp3播放器主要完成下列功能:
1. 添加歌曲,可以添加單個樂曲或者指定文件夾內(nèi)包括其子文件夾內(nèi)的所有mp3樂曲到播放列表。
2. 刪除指定歌曲或所有歌曲。
3. 播放的控制。包括選擇上一首,下一首播放,順序播放,循環(huán)播放和隨機播放。循環(huán)播放又分單個歌曲的循環(huán)播放和所有歌曲的循環(huán)播放。
首先建立類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 重復(fù)播放全部
type = 2 重復(fù)播放一首
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;
}
}
}
Mp3播放器主要完成下列功能:
1. 添加歌曲,可以添加單個樂曲或者指定文件夾內(nèi)包括其子文件夾內(nèi)的所有mp3樂曲到播放列表。
2. 刪除指定歌曲或所有歌曲。
3. 播放的控制。包括選擇上一首,下一首播放,順序播放,循環(huán)播放和隨機播放。循環(huán)播放又分單個歌曲的循環(huán)播放和所有歌曲的循環(huán)播放。
首先建立類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 重復(fù)播放全部
type = 2 重復(fù)播放一首
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;
}
}
}