扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:中国IT实验室 来源:中国IT实验室 2007年9月27日
关键字: MP3 Media 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;
}
}
}
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);
}
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);
}
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);
}
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);
}
}
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者