public partial class Form1 : Form { public Form1() { InitializeComponent(); } /// <summary> /// 点击播放上一曲 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button2_Click(object sender, EventArgs e) { //获得当前选中歌曲的索引 int index = listBox1.SelectedIndex; index--; if (index < 0) { index = listBox1.Items.Count - 1; } listBox1.SelectedIndex = index; sp.SoundLocation = list[index]; sp.Play(); } /// <summary> /// 点击播放下一曲 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> 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 = list[index]; sp.Play(); } //用来存储音乐文件的全路径 List<string> list = new List<string>(); private void btnOpen_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Title = "请选择音乐文件"; ofd.InitialDirectory = @"C:\Users\hhlt_00\Desktop\Music"; ofd.Multiselect = true; ofd.Filter = "音乐文件|*.wav|所有文件|*.*"; ofd.ShowDialog(); //获得我们在文件夹中所选择的所有文件的全路径 string[] path = ofd.FileNames; for (int i = 0; i < path.Length; i++) { //将音乐文件的文件名加载到listBox中 listBox1.Items.Add(Path.GetFileName(path[i])); //将音乐文件的全路径放到泛型集合里面 list.Add(path[i]); } } /// <summary> /// 实现双击播放 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> /// SoundPlayer sp = new SoundPlayer(); private void listBox1_DoubleClick(object sender, EventArgs e) { sp.SoundLocation = list[listBox1.SelectedIndex]; sp.Play(); } }
时间: 2024-10-26 09:28:40