音乐播放器
功能: 1.选择文件夹里的音乐文件 2.根据选中的索引切换播放 3.根据索引让音乐文件显示他被选中了 4个按钮一个listbox
結果展示:
用来存储音乐文件的全路径 局部变量拿不到
代码语言:javascript复制List<string> listSongs = new List<string>();
打开按钮是选择文件路径
代码语言:javascript复制private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog odf = new OpenFileDialog();
odf.Title = "选择音乐文件夹";
odf.InitialDirectory = @"C:UsersH0772690Music";
odf.Multiselect = true;//多选
odf.Filter = "音乐文件|*.wav|所有文件|*.*";
odf.ShowDialog();
//获得我们在文件夹中选择的所有文件的全路径
string[] path = odf.FileNames;
for (int i = 0; i < path.Length; i )
{
listBox1.Items.Add(path[i]);
//将音乐文件的全路径存储到泛型集合中
listSongs.Add(path[i]);
}
}
listbox
代码语言:javascript复制 /// <summary>
/// 实现双击播放
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
SoundPlayer sp = new SoundPlayer();
private void listBox1_DoubleClick(object sender, EventArgs e)
{
sp.SoundLocation = listSongs[listBox1.SelectedIndex];
sp.Play();
}
关闭按钮
代码语言:javascript复制private void button4_Click(object sender, EventArgs e)
{
this.Close();
}
上一曲
代码语言:javascript复制private void button2_Click(object sender, EventArgs e)
{
int index = listBox1.SelectedIndex;
if (index == 0)
{
index = listBox1.Items.Count;
}
index--;
//将改变后的索引从新赋值给我选中项的索引
listBox1.SelectedIndex = index;
sp.SoundLocation = listSongs[index];
sp.Play();
}
下一曲
代码语言:javascript复制private void button3_Click(object sender, EventArgs e)
{
int index = listBox1.SelectedIndex;
index ;
if (index == listBox1.Items.Count)
{
index = 0;
}
//将改变后的索引从新赋值给我选中项的索引
listBox1.SelectedIndex = index;
sp.SoundLocation = listSongs[index];
sp.Play();
}