实现流程
1.1、创建项目
1.2、准备素材
1.3、功能开发
public void addLocalSong(string[] files)
{
XmlDocument xd = new XmlDocument();
xd.Load(Directory.GetCurrentDirectory() + "\\data\\" + "music.xml");
XmlElement xe = xd.DocumentElement;
int i = xe.ChildNodes.Count;
foreach (string str in files)
{
i++;
XmlNode x = xd.CreateElement("path");
x.InnerText = str;
xe.AppendChild(x);
string[] t = { i.ToString(), Path.GetFileNameWithoutExtension(str) };
ListViewItem lvi = new ListViewItem(t);
this.listView1.Items.Add(lvi);
}
xd.Save(Directory.GetCurrentDirectory() + "\\data\\" + "music.xml");
}
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "音乐|*.mp3";
ofd.Multiselect = true;
ofd.Title = "本地歌曲添加";
DialogResult dr = ofd.ShowDialog();
if (dr == DialogResult.OK)
{
addLocalSong(ofd.FileNames);
}
/// <summary>
/// 播放或暂停歌曲
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
if (this.now_music_id == -1)
return;
if (this.button2.Text == "播放" && this.trackBar1.Value < (this.trackBar1.Maximum - 2))
{
this.timer1.Enabled = true;
this.Mp3Player.Ctlcontrols.play();
this.button2.Text = "暂停";
}
else if (this.button2.Text == "播放" && this.trackBar1.Value >= (this.trackBar1.Maximum - 2))
{
this.timer1.Enabled = true;
this.button2.Text = "暂停";
this.PlayModeChange();
}
else
{
this.timer1.Enabled = false;
this.Mp3Player.Ctlcontrols.pause();
this.button2.Text = "播放";
}
}
if (this.listView1.SelectedItems.Count == 0)
return;
this.button2.Text = "暂停";
this.timer1.Enabled = true;
ListViewItem lvi = this.listView1.SelectedItems[0];
//MessageBox.Show("双击");
this.label3.Text = this.listView1.SelectedItems[0].SubItems[1].Text;
this.label3.Text = lvi.SubItems[1].Text;
if (this.label3.Text.Length > 15)
this.label3.Text = this.label3.Text.Substring(0, 13) + "..";
this.label3.Location = new Point((int)(this.panel1.Width / 2 - this.label3.Width / 2), this.label3.Location.Y);
this.now_music_id = Convert.ToInt32(lvi.SubItems[0].Text) - 1;
XmlDocument xd = new XmlDocument();
xd.Load(Directory.GetCurrentDirectory() + "\\data\\" + "music.xml");
string filename = xd.DocumentElement.ChildNodes[this.now_music_id].InnerText;
this.musicPlay(filename);
string lrc_filename = Path.GetDirectoryName(filename) + "\\" + Path.GetFileNameWithoutExtension(filename) + ".lrc";
if (File.Exists(lrc_filename))
{
loadLrc(lrc_filename);
}
else
{
this.richTextBox1.Text = "歌词文件不存在";
}
if (this.now_music_id == -1)
return;
this.Mp3Player.Ctlcontrols.currentPosition = this.trackBar1.Value;
this.label1.Text = this.Mp3Player.Ctlcontrols.currentPositionString;
if (this.trackBar1.Value >= (this.trackBar1.Maximum - 2) && this.button2.Text == "暂停")
{
this.PlayModeChange();
}
private void trackBar2_Scroll(object sender, EventArgs e)
{
if (this.now_music_id == -1)
return;
this.Mp3Player.settings.volume = this.trackBar2.Value;
}