【IT168技术】闲来没事自己做了一个基于WindowsMediaPlayer的迷你音乐播放器,界面如下图
功能简介:
1.循环模式:顺不播放
axWMusicPlayer.settings.setMode("shuffle", false);
全部循环
axWMusicPlayer.settings.setMode("loop", true);
随机播放
axWMusicPlayer.settings.setMode("shuffle", true);
2.模拟定时关机
程序写到定时关机,具体的调用定时关机程序省略了,程序中相应地方有注解。
定时关机代码
1 private void 关机时间toolStripTextBox_KeyPress(object sender, KeyPressEventArgs e)
2 {
3 if (e.KeyChar == (char)Keys.Enter && 关机时间toolStripTextBox.Text != "")
4 {
5 try
6 {
7 DateTime time = DateTime.Parse(关机时间toolStripTextBox.Text);
8 TimeSpan span = new TimeSpan(time.Hour, time.Minute, 0);
9 if (span.CompareTo(new TimeSpan(DateTime.Now.Hour, DateTime.Now.Minute, 0)) == 0) //当前关机时间
10 {
11 if (MessageBox.Show("你设定的关机时间是当前计算机时间,是否直接关机?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.OK)
12 {
13 MynotifyIcon.Icon = Icon.ExtractAssociatedIcon(PathBase + "\\Images\\" + "ShutDown_notifyIcon.ico");
14 //
15 //调用关机程序
16 //
17 }
18 else
19 {
20 return;
21 }
22 }
23 if (span.CompareTo(new TimeSpan(DateTime.Now.Hour, DateTime.Now.Minute, 0)) < 0) //过去关机时间
24 {
25 MessageBox.Show("此时间已是过去时间,设定无效", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
26 return;
27 }
28 if (span.CompareTo(new TimeSpan(DateTime.Now.Hour, DateTime.Now.Minute, 0)) > 0) //有效关机时间
29 {
30
31 ShutDownTime = 关机时间toolStripTextBox.Text;
32 MessageBox.Show("成功设置定时关机,计算机将于“" + ShutDownTime + "”关机…… *︶︵︶*", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
33 this.关机时间toolStripTextBox.Visible = false;
34 this.取消定时关机QToolStripMenuItem.Visible = true;
35
36 //
37 //调用关机程序
38 //
39 }
40 }
41 catch
42 {
43 MessageBox.Show("日期格式不正确,请重新输入。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
44 return;
45 }
46 }
47 if (e.KeyChar == (char)Keys.Enter && 关机时间toolStripTextBox.Text == "")
48 {
49 关机时间toolStripTextBox.Visible = false;
50 }
51 }
1 private void 关机时间toolStripTextBox_KeyPress(object sender, KeyPressEventArgs e)
2 {
3 if (e.KeyChar == (char)Keys.Enter && 关机时间toolStripTextBox.Text != "")
4 {
5 try
6 {
7 DateTime time = DateTime.Parse(关机时间toolStripTextBox.Text);
8 TimeSpan span = new TimeSpan(time.Hour, time.Minute, 0);
9 if (span.CompareTo(new TimeSpan(DateTime.Now.Hour, DateTime.Now.Minute, 0)) == 0) //当前关机时间
10 {
11 if (MessageBox.Show("你设定的关机时间是当前计算机时间,是否直接关机?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.OK)
12 {
13 MynotifyIcon.Icon = Icon.ExtractAssociatedIcon(PathBase + "\\Images\\" + "ShutDown_notifyIcon.ico");
14 //
15 //调用关机程序
16 //
17 }
18 else
19 {
20 return;
21 }
22 }
23 if (span.CompareTo(new TimeSpan(DateTime.Now.Hour, DateTime.Now.Minute, 0)) < 0) //过去关机时间
24 {
25 MessageBox.Show("此时间已是过去时间,设定无效", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
26 return;
27 }
28 if (span.CompareTo(new TimeSpan(DateTime.Now.Hour, DateTime.Now.Minute, 0)) > 0) //有效关机时间
29 {
30
31 ShutDownTime = 关机时间toolStripTextBox.Text;
32 MessageBox.Show("成功设置定时关机,计算机将于“" + ShutDownTime + "”关机…… *︶︵︶*", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
33 this.关机时间toolStripTextBox.Visible = false;
34 this.取消定时关机QToolStripMenuItem.Visible = true;
35
36 //
37 //调用关机程序
38 //
39 }
40 }
41 catch
42 {
43 MessageBox.Show("日期格式不正确,请重新输入。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
44 return;
45 }
46 }
47 if (e.KeyChar == (char)Keys.Enter && 关机时间toolStripTextBox.Text == "")
48 {
49 关机时间toolStripTextBox.Visible = false;
50 }
51 }
3.歌曲列表信息保存在XML文件中
添加歌曲保存代码
1 private void btnList_Click(object sender, EventArgs e) //添加歌曲
2 {
3 openFileDialog1.Title = "添加歌曲";
4 openFileDialog1.FileName = "";
5 openFileDialog1.Multiselect = true;
6 openFileDialog1.Filter = "Mp3文件|*.mp3|Wav文件|*.wav|Wma文件|*.wma|Wmv文件|*.wmv|所有格式|*.*";
7 openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyMusic);
8 try
9 {
10 if (openFileDialog1.ShowDialog() == DialogResult.OK)
11 {
12 //IsCreateXmlFile();
13 XmlDocument xmldoc = new XmlDocument();
14 xmldoc.Load(xmlfile);
15 XmlNode root = xmldoc.SelectSingleNode("MusicList");
16 string[] FileNamesList = openFileDialog1.FileNames;
17 foreach (string file in FileNamesList)
18 {
19 string filename= Path.GetFileName(file).Substring(0,Path.GetFileName(file).LastIndexOf('.'));
20 axWMusicPlayer.currentPlaylist.appendItem(axWMusicPlayer.newMedia(file));
21 XmlElement newelement = xmldoc.CreateElement("MusicProperty");
22 newelement.SetAttribute("MusicUrl", file);
23 newelement.SetAttribute("MusicName", filename);
24 newelement.SetAttribute("LikeCount", "0");
25 root.AppendChild(newelement);
26 Application.DoEvents();
27 }
28 xmldoc.Save(xmlfile);
29 }
30 }
31 catch(Exception ex)
32 {
33 MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
34 }
35 }
1 private void btnList_Click(object sender, EventArgs e) //添加歌曲
2 {
3 openFileDialog1.Title = "添加歌曲";
4 openFileDialog1.FileName = "";
5 openFileDialog1.Multiselect = true;
6 openFileDialog1.Filter = "Mp3文件|*.mp3|Wav文件|*.wav|Wma文件|*.wma|Wmv文件|*.wmv|所有格式|*.*";
7 openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyMusic);
8 try
9 {
10 if (openFileDialog1.ShowDialog() == DialogResult.OK)
11 {
12 //IsCreateXmlFile();
13 XmlDocument xmldoc = new XmlDocument();
14 xmldoc.Load(xmlfile);
15 XmlNode root = xmldoc.SelectSingleNode("MusicList");
16 string[] FileNamesList = openFileDialog1.FileNames;
17 foreach (string file in FileNamesList)
18 {
19 string filename= Path.GetFileName(file).Substring(0,Path.GetFileName(file).LastIndexOf('.'));
20 axWMusicPlayer.currentPlaylist.appendItem(axWMusicPlayer.newMedia(file));
21 XmlElement newelement = xmldoc.CreateElement("MusicProperty");
22 newelement.SetAttribute("MusicUrl", file);
23 newelement.SetAttribute("MusicName", filename);
24 newelement.SetAttribute("LikeCount", "0");
25 root.AppendChild(newelement);
26 Application.DoEvents();
27 }
28 xmldoc.Save(xmlfile);
29 }
30 }
31 catch(Exception ex)
32 {
33 MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
34 }
35 }
4.主界面上的喜爱按钮实现的是歌曲排列顺序
在下次启动程序后,程序会自动将喜爱点击次数多的歌曲排到前面
喜爱按钮事件
1 private void btnFavorite_Click(object sender, EventArgs e) //喜爱
2 {
3 if (axWMusicPlayer.currentMedia != null)
4 {
5 XmlDocument doc = new XmlDocument();
6 doc.Load(xmlfile);
7 XmlNodeList nodelist = doc.SelectSingleNode("MusicList").ChildNodes;
8 foreach (XmlNode node in nodelist)
9 {
10 XmlElement element = (XmlElement)node; //将XmlNode节点node转化成XmlElement型的node
11 if (element.GetAttribute("MusicUrl") == axWMusicPlayer.currentMedia.sourceURL)
12 {
13 int likecount = Convert.ToInt32(element.GetAttribute("LikeCount")) + 1;
14 element.SetAttribute("LikeCount", likecount.ToString());
15 }
16 }
17 doc.Save(xmlfile);
18 }
19 }
1 private void btnFavorite_Click(object sender, EventArgs e) //喜爱
2 {
3 if (axWMusicPlayer.currentMedia != null)
4 {
5 XmlDocument doc = new XmlDocument();
6 doc.Load(xmlfile);
7 XmlNodeList nodelist = doc.SelectSingleNode("MusicList").ChildNodes;
8 foreach (XmlNode node in nodelist)
9 {
10 XmlElement element = (XmlElement)node; //将XmlNode节点node转化成XmlElement型的node
11 if (element.GetAttribute("MusicUrl") == axWMusicPlayer.currentMedia.sourceURL)
12 {
13 int likecount = Convert.ToInt32(element.GetAttribute("LikeCount")) + 1;
14 element.SetAttribute("LikeCount", likecount.ToString());
15 }
16 }
17 doc.Save(xmlfile);
18 }
19 }
5、点击进度条歌曲直接跳到当前位置继续播放
设置当前播放位置
private void pictureBox2_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
axWMusicPlayer.Ctlcontrols.currentPosition = e.X * axWMusicPlayer.currentMedia.duration / 170;
}
}
{
if (e.Button == MouseButtons.Left)
{
axWMusicPlayer.Ctlcontrols.currentPosition = e.X * axWMusicPlayer.currentMedia.duration / 170;
}
}
注解:170为进度条总长度