前言
現在網上有很多的音樂播放器,但好像都不是.net平台做的,在.net中實現音樂文件的播放功能很簡單,下面就簡單實現下。
SoundPlayer類
在.net提供了音樂文件的類:SoundPlayer,但是只支持.wav格式,我們用的時候一般是播放系統文件,比如一些提示聲音等。代碼很簡單:
1 SoundPlayer player = new SoundPlayer(); 2 player.SoundLocation = filePath;//filePath文件的物理路徑 3 player.Play();
使用這個類我們做個小程序,如下:
WMP組件
上面的小程序有幾個問題:不支持暫停,進度顯示,文件格式限制等,因為SoundPlayer就是來播放系統文件的,不能要求它那么全面。因為windows系統自帶播放器windows media player,我們可以用使用其自帶的組件來實現上面的要求,做起來也很簡單。
先引用Windows Media Player組件,然后就像拖控件一樣用就行了。效果如圖:
代碼實現起來很簡單,播放,暫停,停止代碼如下:
1 wmp.Ctlcontrols.play();//播放 2 wmp.Ctlcontrols.pause();//暫停 3 wmp.Ctlcontrols.stop();//停止
完整代碼:

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.Windows.Forms; 9 10 using WMPLib; 11 using System.Media; 12 using System.IO; 13 using System.Threading; 14 15 namespace 音樂播放器 16 { 17 public partial class Form2 : Form 18 { 19 public int index = 1; 20 public int listIndex; 21 SoundPlayer player = new SoundPlayer(); 22 Dictionary<string, string> dic = new Dictionary<string, string>(); 23 IWMPMedia media; 24 public Form2() 25 { 26 InitializeComponent(); 27 } 28 29 private void Form2_Load(object sender, EventArgs e) 30 { 31 32 } 33 34 /// <summary> 35 /// 導入文件 36 /// </summary> 37 private void tsmiLoading_Click(object sender, EventArgs e) 38 { 39 string fileName, fileExtension, fileSize, temp; 40 FileInfo fi = null; 41 ListViewItem lvi = null; 42 43 openFileDialog1.Multiselect = true; 44 openFileDialog1.Filter = "mp3文件(*.mp3)|*.mp3|wav文件(*.wav)|*.wav|wma文件(*.wma)|*.wma"; 45 if (openFileDialog1.ShowDialog() == DialogResult.OK) 46 { 47 foreach (string filePath in openFileDialog1.FileNames) 48 { 49 fi = new FileInfo(filePath); 50 temp = filePath.Remove(filePath.LastIndexOf('.')); 51 fileName = temp.Remove(0, temp.LastIndexOf('\\') + 1); 52 fileExtension = filePath.Remove(0, filePath.LastIndexOf('.')); 53 fileSize = (fi.Length / 1024).ToString() + "KB"; 54 55 lvi = new ListViewItem(); 56 lvi.Text = index++.ToString(); 57 lvi.SubItems.AddRange(new string[] { fileName, fileExtension, fileSize, filePath }); 58 59 if (lvDetail.Items.Count > 0) 60 { 61 if (!dic.ContainsKey(filePath)) 62 { 63 lvDetail.Items.Add(lvi); 64 dic.Add(filePath, fileName); 65 //添加到播放列表 66 media = wmp.newMedia(filePath); 67 wmp.currentPlaylist.insertItem(listIndex++, media); 68 } 69 else 70 { 71 index--; 72 MessageBox.Show("文件已存在!"); 73 } 74 } 75 else 76 { 77 lvDetail.Items.Add(lvi); 78 dic.Add(filePath, fileName); 79 //添加到播放列表 80 media = wmp.newMedia(filePath); 81 wmp.currentPlaylist.insertItem(listIndex++, media); 82 } 83 } 84 } 85 } 86 87 /// <summary> 88 /// 清除列表 89 /// </summary> 90 private void tsmiClear_Click(object sender, EventArgs e) 91 { 92 lvDetail.Items.Clear(); 93 } 94 95 /// <summary> 96 /// 播放 97 /// </summary> 98 private void tsmiPlayer_Click(object sender, EventArgs e) 99 { 100 wmp.Ctlcontrols.play(); 101 } 102 103 /// <summary> 104 /// 暫停 105 /// </summary> 106 private void tsmiWaiting_Click(object sender, EventArgs e) 107 { 108 wmp.Ctlcontrols.pause(); 109 } 110 111 /// <summary> 112 /// 停止 113 /// </summary> 114 private void tsmiStop_Click(object sender, EventArgs e) 115 { 116 wmp.Ctlcontrols.stop(); 117 } 118 119 /// <summary> 120 /// 退出 121 /// </summary> 122 private void tsmiExit_Click(object sender, EventArgs e) 123 { 124 Application.Exit(); 125 } 126 127 /// <summary> 128 /// 雙擊某項播放 129 /// </summary> 130 private void lvDetail_DoubleClick(object sender, EventArgs e) 131 { 132 if (lvDetail.SelectedItems.Count > 0) 133 { 134 wmp.currentMedia = wmp.currentPlaylist.Item[int.Parse(lvDetail.SelectedItems[0].Text) - 1]; 135 } 136 } 137 } 138 }
程序下載:音樂播放器.rar