using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Media; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace 音樂播放器 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } List<string> Music = new List<string>(); private void button1_Click(object sender, EventArgs e) { //打開一個對話框,提示用戶選擇文件 OpenFileDialog ofd = new OpenFileDialog(); ofd.Title = ("請選擇音樂文件"); //文本框標題 ofd.InitialDirectory = @"D:\FFOutput"; //默認打開的文件夾 ofd.Multiselect = true; //允許對話框選擇多個文件 ofd.Filter = "音樂文件|*.wav|所有文件|*.*"; //可選的文件類型 ofd.ShowDialog(); //顯示對話框 //將所有選中的文件添加到listbox中 string[] path = ofd.FileNames; //獲取被選擇文件的全路徑 for (int i = 0; i < path.Length; i++) { listBox1.Items.Add(Path.GetFileName(path[i])); //將音樂文件添加到ListBox中 Music.Add(path[i]); //將音樂文件的全路徑添加到List集合中 } } SoundPlayer sp = new SoundPlayer(); //播放對象 //雙擊歌曲播放 private void listBox1_DoubleClick(object sender, EventArgs e) { sp.SoundLocation = Music[listBox1.SelectedIndex]; //ListBox1中選中的文件的索引 sp.Play(); } //下一曲 private void button3_Click(object sender, EventArgs e) { int index = listBox1.SelectedIndex; //獲取當前選中歌曲的索引 //if(index != listBox1.Items.Count-1) //{ // index++; //下一曲時索引自增 //} //else if(index==listBox1.Items.Count-1) //當前選中歌曲索引為最后一首歌時,重新回到第一曲 //{ // index = 0; //} index++; if(index == listBox1.Items.Count) { index = 0; } sp.SoundLocation = Music[index]; listBox1.SelectedIndex = index; sp.Play(); } //上一曲 private void button2_Click(object sender, EventArgs e) { int index = listBox1.SelectedIndex; //獲取當前選中歌曲的索引 //if(index != 0) //{ // index--; //上一曲時索引自增 //} //else if (index == 0) //當前選中歌曲索引為最后一首歌時,重新回到第一曲 //{ // index = listBox1.Items.Count-1; //} if (index == 0) { index = listBox1.Items.Count-1; } else { index--; } sp.SoundLocation = Music[index]; listBox1.SelectedIndex = index; sp.Play(); } private void button4_Click(object sender, EventArgs e) { //Thread.Sleep(); } } }