右键组件,选择选择项
    image.png
    点击COM组件,选择Windows Media Player
    image.png

    正文:
    image.png

    image.png

    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Data;
    5. using System.Drawing;
    6. using System.IO;
    7. using System.Linq;
    8. using System.Text;
    9. using System.Threading.Tasks;
    10. using System.Windows.Forms;
    11. namespace _134_WinForm复习___播放器
    12. {
    13. public partial class Form1 : Form
    14. {
    15. public Form1()
    16. {
    17. InitializeComponent();
    18. }
    19. bool b = true;
    20. /// <summary>
    21. /// 播放或者暂停
    22. /// </summary>
    23. /// <param name="sender"></param>
    24. /// <param name="e"></param>
    25. private void btnPlayorPause_Click(object sender, EventArgs e)
    26. {
    27. if (btnPlayorPause.Text == "播放")
    28. {
    29. if (b)
    30. {
    31. //获得选中的歌曲的地址
    32. musicPlayer.URL = listPath[listBoxMusic.SelectedIndex];
    33. }
    34. musicPlayer.Ctlcontrols.play();
    35. btnPlayorPause.Text = "暂停";
    36. }
    37. else if (btnPlayorPause.Text == "暂停")
    38. {
    39. musicPlayer.Ctlcontrols.pause();
    40. btnPlayorPause.Text = "播放";
    41. b = false;
    42. }
    43. }
    44. private void Form1_Load(object sender, EventArgs e)
    45. {
    46. //在程序加载的时候,取消播放器的自动播放功能
    47. musicPlayer.settings.autoStart = false;
    48. //musicPlayer.URL = @"C:\Users\46124\Desktop\.wav音乐\织风结.m4a";
    49. lblVolume.Text = musicPlayer.settings.volume.ToString();
    50. }
    51. private void tbnStop_Click(object sender, EventArgs e)
    52. {
    53. musicPlayer.Ctlcontrols.stop();
    54. }
    55. //存储音乐文件的全路径
    56. List<string> listPath = new List<string>();
    57. /// <summary>
    58. /// 打开对话框,选择音乐
    59. /// </summary>
    60. /// <param name="sender"></param>
    61. /// <param name="e"></param>
    62. private void btnChooseMusic_Click(object sender, EventArgs e)
    63. {
    64. OpenFileDialog ofd = new OpenFileDialog();
    65. ofd.InitialDirectory = @"C:\Users\46124\Desktop\.wav音乐";
    66. ofd.Title = "客官,请选择你要听的音乐";
    67. ofd.Filter = "WAV文件|*.wav|M4A文件|*.m4a|MP3文件|*.mp3|MP4文件|*.mp4|所有文件|*.*";
    68. ofd.Multiselect = true;
    69. ofd.ShowDialog();
    70. //获得在文本框中选择文件的全路径
    71. string[] path = ofd.FileNames;
    72. for (int i = 0; i < path.Length; i++)
    73. {
    74. //将获得在文本框中选择文件的全路径储存到泛型集合中
    75. listPath.Add(path[i]);
    76. //将音乐文件的文件名储存到ListBox中
    77. listBoxMusic.Items.Add(Path.GetFileName(path[i]));
    78. }
    79. }
    80. /// <summary>
    81. /// 双击播放音乐
    82. /// </summary>
    83. /// <param name="sender"></param>
    84. /// <param name="e"></param>
    85. private void listBoxMusic_DoubleClick(object sender, EventArgs e)
    86. {
    87. if (listBoxMusic.Items.Count == 0)
    88. {
    89. MessageBox.Show("请选择音乐文件!");
    90. return;
    91. }
    92. try
    93. {
    94. musicPlayer.URL = listPath[listBoxMusic.SelectedIndex];
    95. musicPlayer.Ctlcontrols.play();
    96. btnPlayorPause.Text = "暂停";
    97. }
    98. catch { }
    99. }
    100. /// <summary>
    101. /// 点击下一曲
    102. /// </summary>
    103. /// <param name="sender"></param>
    104. /// <param name="e"></param>
    105. private void btnNext_Click(object sender, EventArgs e)
    106. {
    107. try
    108. {
    109. //获得当前项的索引
    110. int index = listBoxMusic.SelectedIndex;
    111. //清空所有选中项的索引(因为将listBox设置为了多选需要这样操作
    112. listBoxMusic.SelectedIndices.Clear();
    113. index++;
    114. if (index == listBoxMusic.Items.Count)
    115. {
    116. index = 0;
    117. }
    118. //将改变后的索引重新赋值给当前选中项的索引(改变蓝条)
    119. listBoxMusic.SelectedIndex = index;
    120. //播放下一曲
    121. musicPlayer.URL = listPath[index];
    122. musicPlayer.Ctlcontrols.play();
    123. btnPlayorPause.Text = "暂停";
    124. }
    125. catch
    126. { MessageBox.Show("请选择文件!"); }
    127. }
    128. /// <summary>
    129. /// 点击上一曲
    130. /// </summary>
    131. /// <param name="sender"></param>
    132. /// <param name="e"></param>
    133. private void btnLast_Click(object sender, EventArgs e)
    134. {
    135. try
    136. {
    137. //获得当前项的索引
    138. int index = listBoxMusic.SelectedIndex;
    139. //清空所有选中项的索引(因为将listBox设置为了多选需要这样操作
    140. listBoxMusic.SelectedIndices.Clear();
    141. index--;
    142. if (index == -1)
    143. {
    144. index = listBoxMusic.Items.Count - 1;
    145. }
    146. //将改变后的索引重新赋值给当前选中项的索引(改变蓝条)
    147. listBoxMusic.SelectedIndex = index;
    148. //播放上一曲
    149. musicPlayer.URL = listPath[index];
    150. musicPlayer.Ctlcontrols.play();
    151. btnPlayorPause.Text = "暂停";
    152. }
    153. catch { MessageBox.Show("请选择文件!"); }
    154. }
    155. /// <summary>
    156. /// 点击删除 选中项
    157. /// </summary>
    158. /// <param name="sender"></param>
    159. /// <param name="e"></param>
    160. private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
    161. {
    162. //先删集合还是删列表
    163. //首先获得要删除歌曲的数量
    164. int count = listBoxMusic.SelectedItems.Count;
    165. for (int i = 0; i < count; i++)
    166. {
    167. //先删集合
    168. listPath.RemoveAt(listBoxMusic.SelectedIndex);
    169. //再删列表
    170. listBoxMusic.Items.RemoveAt(listBoxMusic.SelectedIndex);
    171. }
    172. //如果先删除列表,那么listBoxMusic.SelectedIndex就会发生变化,集合会按照这发生的listBoxMusic.SelectedIndex去进行删除,就会报错。
    173. }
    174. /// <summary>
    175. /// 减小音量
    176. /// </summary>
    177. /// <param name="sender"></param>
    178. /// <param name="e"></param>
    179. private void button1_Click(object sender, EventArgs e)
    180. {
    181. musicPlayer.settings.volume -= 5;
    182. lblVolume.Text = musicPlayer.settings.volume.ToString();
    183. }
    184. /// <summary>
    185. /// 放大音量
    186. /// </summary>
    187. /// <param name="sender"></param>
    188. /// <param name="e"></param>
    189. private void button2_Click(object sender, EventArgs e)
    190. {
    191. musicPlayer.settings.volume += 5;
    192. lblVolume.Text = musicPlayer.settings.volume.ToString();
    193. }
    194. /// <summary>
    195. /// 点击静音
    196. /// </summary>
    197. /// <param name="sender"></param>
    198. /// <param name="e"></param>
    199. private void btnMute_Click(object sender, EventArgs e)
    200. {
    201. musicPlayer.settings.mute = true;
    202. }
    203. private void timer1_Tick(object sender, EventArgs e)
    204. {
    205. //如果播放器的状态等于正在播放中
    206. if (musicPlayer.playState == WMPLib.WMPPlayState.wmppsPlaying)
    207. {
    208. lblInfomation.Text = musicPlayer.currentMedia.duration.ToString() + "\r\n"
    209. + musicPlayer.currentMedia.durationString + "\r\n"
    210. + musicPlayer.Ctlcontrols.currentPosition.ToString() + "\r\n"
    211. + musicPlayer.Ctlcontrols.currentPositionString;
    212. //比较总时间和当前播放时间
    213. if (musicPlayer.currentMedia.duration <= musicPlayer.Ctlcontrols.currentPosition + 1)
    214. {
    215. //下一曲
    216. btnNext_Click(sender, e);
    217. }
    218. }
    219. //if (musicPlayer.playState== WMPLib.WMPPlayState.wmppsReady)
    220. //{
    221. // musicPlayer.Ctlcontrols.play();
    222. //}
    223. //if (musicPlayer.playState!= WMPLib.WMPPlayState.wmppsPlaying)
    224. //{
    225. // //下一曲
    226. // btnNext_Click(sender, e);
    227. //}
    228. }
    229. }
    230. }

    image.png
    //先删集合还是删列表
    //如果先删除列表,那么listBoxMusic.SelectedIndex就会发生变化,集合会按照这发生的listBoxMusic.SelectedIndex去进行删除,就会报错。

    用label实现静\放音
    image.png