01复习
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 using System.IO; 11 using System.Media; 12 namespace _01复习 13 { 14 public partial class Form1 : Form 15 { 16 public Form1() 17 { 18 InitializeComponent(); 19 } 20 //用来存储音乐文件的全路径 21 List<string> listSongs = new List<string>(); 22 private void button1_Click(object sender, EventArgs e) 23 { 24 OpenFileDialog ofd = new OpenFileDialog(); 25 ofd.Title = "请选择音乐文件"; 26 ofd.InitialDirectory = @"F:\CloudMusic"; 27 ofd.Multiselect = true; 28 ofd.Filter = "音乐文件|*.wav|所有文件|*.*"; 29 ofd.ShowDialog(); 30 //获得我们在文件夹中选择所有文件的全路径 31 string[] path = ofd.FileNames; 32 for (int i = 0; i < path.Length; i++) 33 { 34 //将音乐文件的文件名加载到ListBox中 35 listBox1.Items.Add(Path.GetFileName(path[i])); 36 //将音乐文件的全路径存储到泛型集合中 37 listSongs.Add(path[i]); 38 } 39 } 40 41 42 /// <summary> 43 /// 实现双击播放 44 /// </summary> 45 /// <param name="sender"></param> 46 /// <param name="e"></param> 47 /// 48 SoundPlayer sp = new SoundPlayer(); 49 private void listBox1_DoubleClick(object sender, EventArgs e) 50 { 51 52 sp.SoundLocation=listSongs[listBox1.SelectedIndex]; 53 sp.Play(); 54 } 55 /// <summary> 56 /// 点击下一曲 57 /// </summary> 58 /// <param name="sender"></param> 59 /// <param name="e"></param> 60 private void button3_Click(object sender, EventArgs e) 61 { 62 //获得当前选中歌曲的索引 63 int index = listBox1.SelectedIndex; 64 index++; 65 if (index == listBox1.Items.Count) 66 { 67 index = 0; 68 } 69 //将改变后的索引重新的赋值给我当前选中项的索引 70 listBox1.SelectedIndex = index; 71 sp.SoundLocation = listSongs[index]; 72 sp.Play(); 73 } 74 75 76 /// <summary> 77 /// 点击上一曲 78 /// </summary> 79 /// <param name="sender"></param> 80 /// <param name="e"></param> 81 private void button2_Click(object sender, EventArgs e) 82 { 83 int index = listBox1.SelectedIndex; 84 index--; 85 if (index < 0) 86 { 87 index = listBox1.Items.Count-1; 88 } 89 //将重新改变后的索引重新的赋值给当前选中项 90 listBox1.SelectedIndex = index; 91 sp.SoundLocation = listSongs[index]; 92 sp.Play(); 93 } 94 95 private void Form1_Load(object sender, EventArgs e) 96 { 97 98 } 99 } 100 }