右键组件,选择选择项
点击COM组件,选择Windows Media Player
正文:

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.IO;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace _134_WinForm复习___播放器{public partial class Form1 : Form{public Form1(){InitializeComponent();}bool b = true;/// <summary>/// 播放或者暂停/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void btnPlayorPause_Click(object sender, EventArgs e){if (btnPlayorPause.Text == "播放"){if (b){//获得选中的歌曲的地址musicPlayer.URL = listPath[listBoxMusic.SelectedIndex];}musicPlayer.Ctlcontrols.play();btnPlayorPause.Text = "暂停";}else if (btnPlayorPause.Text == "暂停"){musicPlayer.Ctlcontrols.pause();btnPlayorPause.Text = "播放";b = false;}}private void Form1_Load(object sender, EventArgs e){//在程序加载的时候,取消播放器的自动播放功能musicPlayer.settings.autoStart = false;//musicPlayer.URL = @"C:\Users\46124\Desktop\.wav音乐\织风结.m4a";lblVolume.Text = musicPlayer.settings.volume.ToString();}private void tbnStop_Click(object sender, EventArgs e){musicPlayer.Ctlcontrols.stop();}//存储音乐文件的全路径List<string> listPath = new List<string>();/// <summary>/// 打开对话框,选择音乐/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void btnChooseMusic_Click(object sender, EventArgs e){OpenFileDialog ofd = new OpenFileDialog();ofd.InitialDirectory = @"C:\Users\46124\Desktop\.wav音乐";ofd.Title = "客官,请选择你要听的音乐";ofd.Filter = "WAV文件|*.wav|M4A文件|*.m4a|MP3文件|*.mp3|MP4文件|*.mp4|所有文件|*.*";ofd.Multiselect = true;ofd.ShowDialog();//获得在文本框中选择文件的全路径string[] path = ofd.FileNames;for (int i = 0; i < path.Length; i++){//将获得在文本框中选择文件的全路径储存到泛型集合中listPath.Add(path[i]);//将音乐文件的文件名储存到ListBox中listBoxMusic.Items.Add(Path.GetFileName(path[i]));}}/// <summary>/// 双击播放音乐/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void listBoxMusic_DoubleClick(object sender, EventArgs e){if (listBoxMusic.Items.Count == 0){MessageBox.Show("请选择音乐文件!");return;}try{musicPlayer.URL = listPath[listBoxMusic.SelectedIndex];musicPlayer.Ctlcontrols.play();btnPlayorPause.Text = "暂停";}catch { }}/// <summary>/// 点击下一曲/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void btnNext_Click(object sender, EventArgs e){try{//获得当前项的索引int index = listBoxMusic.SelectedIndex;//清空所有选中项的索引(因为将listBox设置为了多选需要这样操作listBoxMusic.SelectedIndices.Clear();index++;if (index == listBoxMusic.Items.Count){index = 0;}//将改变后的索引重新赋值给当前选中项的索引(改变蓝条)listBoxMusic.SelectedIndex = index;//播放下一曲musicPlayer.URL = listPath[index];musicPlayer.Ctlcontrols.play();btnPlayorPause.Text = "暂停";}catch{ MessageBox.Show("请选择文件!"); }}/// <summary>/// 点击上一曲/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void btnLast_Click(object sender, EventArgs e){try{//获得当前项的索引int index = listBoxMusic.SelectedIndex;//清空所有选中项的索引(因为将listBox设置为了多选需要这样操作listBoxMusic.SelectedIndices.Clear();index--;if (index == -1){index = listBoxMusic.Items.Count - 1;}//将改变后的索引重新赋值给当前选中项的索引(改变蓝条)listBoxMusic.SelectedIndex = index;//播放上一曲musicPlayer.URL = listPath[index];musicPlayer.Ctlcontrols.play();btnPlayorPause.Text = "暂停";}catch { MessageBox.Show("请选择文件!"); }}/// <summary>/// 点击删除 选中项/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void 删除ToolStripMenuItem_Click(object sender, EventArgs e){//先删集合还是删列表//首先获得要删除歌曲的数量int count = listBoxMusic.SelectedItems.Count;for (int i = 0; i < count; i++){//先删集合listPath.RemoveAt(listBoxMusic.SelectedIndex);//再删列表listBoxMusic.Items.RemoveAt(listBoxMusic.SelectedIndex);}//如果先删除列表,那么listBoxMusic.SelectedIndex就会发生变化,集合会按照这发生的listBoxMusic.SelectedIndex去进行删除,就会报错。}/// <summary>/// 减小音量/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void button1_Click(object sender, EventArgs e){musicPlayer.settings.volume -= 5;lblVolume.Text = musicPlayer.settings.volume.ToString();}/// <summary>/// 放大音量/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void button2_Click(object sender, EventArgs e){musicPlayer.settings.volume += 5;lblVolume.Text = musicPlayer.settings.volume.ToString();}/// <summary>/// 点击静音/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void btnMute_Click(object sender, EventArgs e){musicPlayer.settings.mute = true;}private void timer1_Tick(object sender, EventArgs e){//如果播放器的状态等于正在播放中if (musicPlayer.playState == WMPLib.WMPPlayState.wmppsPlaying){lblInfomation.Text = musicPlayer.currentMedia.duration.ToString() + "\r\n"+ musicPlayer.currentMedia.durationString + "\r\n"+ musicPlayer.Ctlcontrols.currentPosition.ToString() + "\r\n"+ musicPlayer.Ctlcontrols.currentPositionString;//比较总时间和当前播放时间if (musicPlayer.currentMedia.duration <= musicPlayer.Ctlcontrols.currentPosition + 1){//下一曲btnNext_Click(sender, e);}}//if (musicPlayer.playState== WMPLib.WMPPlayState.wmppsReady)//{// musicPlayer.Ctlcontrols.play();//}//if (musicPlayer.playState!= WMPLib.WMPPlayState.wmppsPlaying)//{// //下一曲// btnNext_Click(sender, e);//}}}}

//先删集合还是删列表
//如果先删除列表,那么listBoxMusic.SelectedIndex就会发生变化,集合会按照这发生的listBoxMusic.SelectedIndex去进行删除,就会报错。
用label实现静\放音
