总结一下,下午半天进行的MP3播放器的开发:
首先,新建一个解决方案:命名MP3solution,设置FormBorderStyle为None,即上面的图标,最大化最小化按钮隐藏,但如图所示还有最大化关闭按钮,这时自己添加的两个PictureBox控件,下面的暂停,停止,静音等也是PictureBox控件设置其Image属性为properties资源中的图片.如何导入图片资源也是一个关键点。我们点击Properties下的Resources文件,然后在添加资源中添加图片资源PNG图片。值得一提的是,图片控件的BackColor属性设置为Transparent,及显示为背景颜色。别的还有两三个label控件,用来显示播放的是哪首音乐,和当前是否播放还是暂停等状态,还有一个用来显示当天播放到几秒钟的状态,两个hscrollBar进度条一个用来显示当前音乐的播放进度,另一个是显示声音的大小。另外引进一个openFileDialog控件和timer控件,分别用来打开音乐源文件和定期检测播放器的运行状态,及时显示到label控件上。
接下来我们来看看,后台代码段:首先定义四个变量,其作用注释如下:
private PointmouseOffset;//记录鼠标坐标
private bool ismouseDown = false;//判断是否按下鼠标
boolflag = false;//判断是播放还是打开选择窗口
static bool MM = true;//记录是否静音
看到窗体最右上角的两个按键最小化和关闭按钮,其代码如下:
//窗体最小化
private void pictureBox1_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}
//关闭按钮
private void pictureBox2_Click(object sender, EventArgs e)
{
Application.Exit();
}
当初在设置form窗体的FormBorderStyle属性为None的时候,其实就意味着目前的窗体是不能用鼠标来移动了,那我们如何正常移动它呢?这个要我们额外用代码来实现:
private void pictureBox3_MouseDown(object sender, MouseEventArgs e)
{
int xOffset; //定义X坐标
int yOffset; //定义Y坐标
if (e.Button == MouseButtons.Left)
{ //如果鼠标的左键点击了,则将坐标分别进行相应的移动
xOffset = -e.X;
yOffset = -e.Y;
mouseOffset = new Point(xOffset,yOffset);
ismouseDown = true; //将mousedowm变量置为true,说明mousedown了
}
}
private void pictureBox3_MouseMove(object sender, MouseEventArgs e)
{ //如果鼠标移动了
if (ismouseDown)
{
//获取坐标的位置
Point MousePos = Control.MousePosition;
MousePos.Offset(mouseOffset.X,mouseOffset.Y);
Location = MousePos; //此时获取的坐标的值即为鼠标的坐标
}
}
private void pictureBox3_MouseUp(object sender, MouseEventArgs e)
{ //如果鼠标按键松开了,则将mousedown属性置为false,说明鼠标的左键已经松开
if (e.Button == MouseButtons.Left)
ismouseDown = false;
}
#endregion
int m = 0;
//打开播放按钮,选择要播放的MP3文件
private void pictureBox4_Click(object sender, EventArgs e)
{
if (!flag) //如果是打开按钮,则打开文件
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
axWindowsMediaPlayer1.URL = openFileDialog1.FileName;
m = 1;
lblSongTitle.Text = "歌曲名称:" + axWindowsMediaPlayer1.currentMedia.getItemInfo("Title");
}
}
else
{
axWindowsMediaPlayer1.Ctlcontrols.play();
}
}
//暂停功能
private void pictureBox5_Click(object sender, EventArgs e)
{
axWindowsMediaPlayer1.Ctlcontrols.pause();
flag = true;
}
//停止功能
private void pictureBox6_Click(object sender, EventArgs e)
{
axWindowsMediaPlayer1.Ctlcontrols.stop();
flag = false;
}
//静音功能
private void pictureBox7_Click(object sender, EventArgs e)
{
if (MM)
{
pictureBox7.Image = (Image)Properties.Resources.音量按钮变色;
axWindowsMediaPlayer1.settings.mute = true;
MM = false;
}
else
{
pictureBox7.Image = (Image)Properties.Resources.音量按钮;
axWindowsMediaPlayer1.settings.mute = false;
MM = true;
}
}
如何动态设置图片的背景色,即当你点击一个按钮时显示的是不同的图片效果,在这里我们可以这只图片控件的背景图片为资源里面的图片
private void pictureBox4_MouseEnter(object sender, EventArgs e)
{
pictureBox4.Image = (Image)Properties.Resources.播放按钮变;
}
private void pictureBox4_MouseLeave(object sender, EventArgs e)
{
pictureBox4.Image = (Image)Properties.Resources.播放按钮;
}
private void pictureBox5_MouseEnter(object sender, EventArgs e)
{
pictureBox5.Image = (Image)Properties.Resources.暂停按钮变;
}
private void pictureBox5_MouseLeave(object sender, EventArgs e)
{
pictureBox5.Image = (Image)Properties.Resources.暂停按钮;
}
private void pictureBox6_MouseEnter(object sender, EventArgs e)
{
pictureBox6.Image = (Image)Properties.Resources.停止按钮变;
}
private void pictureBox6_MouseLeave(object sender, EventArgs e)
{
pictureBox6.Image = (Image)Properties.Resources.停止按钮;
}
Timer这个类用来定期检测播放器的状态,以便及时更新信息,显示给用户看
private void timer1_Tick(object sender, EventArgs e)
{
int i = (int)axWindowsMediaPlayer1.playState;
switch (i)
{
case 1: lblStauts.Text = "状态:停止"; break;
case 2: lblStauts.Text = "状态:暂停"; break;
case 3: lblStauts.Text = "状态:播放"; break;
case 6: lblStauts.Text = "状态:正在缓冲"; break;
case 9: lblStauts.Text = "状态:正在连接"; break;
case 10: lblStauts.Text = "状态:准备就绪"; break;
}
lbljindu.Text = axWindowsMediaPlayer1.Ctlcontrols.currentPositionString;
if (m == 1)
{
hScrollBar1.Maximum = (int)axWindowsMediaPlayer1.currentMedia.duration;
hScrollBar1.Minimum = 0;
hScrollBar1.Value = (int)axWindowsMediaPlayer1.Ctlcontrols.currentPosition;
hScrollBar2.Value = axWindowsMediaPlayer1.settings.volume;
}
}
//音乐播放进程条,用来显示已经播放了多少了
private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
axWindowsMediaPlayer1.Ctlcontrols.currentPosition = e.NewValue;
}
//声音进程条
private void hScrollBar2_Scroll(object sender, ScrollEventArgs e)
{
axWindowsMediaPlayer1.settings.volume = e.NewValue;
}
}
}