利用C#制造一个播放器
2010-03-09 01:05
295 查看
利用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中的AddFile和DelFile函数
实现播放上一首
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);
}
播放的控制
利用Player的NextPlay方法返回的值来选择下一次播放的内容。
同时利用PlayStateChange事件来实现由一曲到下一曲的替换,但是在响应PlayStateChange事件的时候直接改变Player的url无法让它直接播放下一曲,解决方法如下:
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,自己的东西感觉就是不一样哦!
本文出自 “落葉” 博客,请务必保留此出处http://dream4728.blog.51cto.com/1299781/281946
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中的AddFile和DelFile函数
实现播放上一首
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);
}
播放的控制
利用Player的NextPlay方法返回的值来选择下一次播放的内容。
同时利用PlayStateChange事件来实现由一曲到下一曲的替换,但是在响应PlayStateChange事件的时候直接改变Player的url无法让它直接播放下一曲,解决方法如下:
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,自己的东西感觉就是不一样哦!
本文出自 “落葉” 博客,请务必保留此出处http://dream4728.blog.51cto.com/1299781/281946
相关文章推荐
- c#利用libvlc.dll写了一个播放器
- 利用移动提供的网站用C#做一个免费发短信的程序(不含源码)
- C#利用Mutex实现只运行一个程序实例
- C#利用NPOI在同一个Excel文件中创建多个sheet
- 一个利用ASC码自动产生a-z文件的C#程序
- 利用vitamio Mediaplayer 自定义了一个播放器
- C#利用反射,遍历获得一个类的所有属性名,以及该类的实例的所有属性的值
- 利用C#编写一个简单的抓网页应用程序
- 利用C#生成一个简单的TIN三角网
- 利用C#制作一个仿IE地址栏的文本框
- 利用MediaElement控件制作一个简单的播放器
- C#新手入门代码 利用legth得到一个数组的长度
- 利用C#编写一个简单的抓网页应用程序
- C#利用反射,遍历获得一个类的所有属性名,以及该类的实例的所有属性的值
- 利用Visual C#.net 创建一个DTS 自定义的任务
- C#中利用函数判断一个数是否为回文…
- 利用Timer控件【Timer(6.4.9)】编写C# Windows应用程序,设计一个如下效果电子时钟。
- C#利用NPOI在同一个Excel文件中创建多个sheet
- 利用Roslyn构建一个简单的C#交互脚本引擎
- 最近利用工作闲余试用了VS2005 Beta2,写了一个播放器.