先从工具箱->右键,组件,选择项->com组件->wmp
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 using System.IO; 10 11 namespace 播放器 12 { 13 public partial class Form1 : Form 14 { 15 public Form1() 16 { 17 InitializeComponent(); 18 } 19 private void btnPlay_Click(object sender, EventArgs e) 20 { 21 //playState判断播放器状态,取值范围是枚举类型。 22 if (musicPlayer.playState == WMPLib.WMPPlayState.wmppsPaused||musicPlayer.playState == WMPLib.WMPPlayState.wmppsStopped 23 || musicPlayer.playState == WMPLib.WMPPlayState.wmppsReady) 24 { 25 musicPlayer.Ctlcontrols.play(); 26 btnPlay.Text = "暂停"; 27 } 28 else 29 { 30 musicPlayer.Ctlcontrols.pause(); 31 btnPlay.Text = "播放"; 32 } 33 } 34 35 private void btnStop_Click(object sender, EventArgs e) 36 { 37 musicPlayer.Ctlcontrols.stop(); 38 } 39 40 private void Form1_Load(object sender, EventArgs e) 41 { 42 musicPlayer.settings.autoStart = false; 43 musicPlayer.URL = @"C:\Users\jjw\Desktop\图片\video\004.mp4"; 44 } 45 46 List<string> list = new List<string>(); 47 private void button1_Click(object sender, EventArgs e) 48 { 49 OpenFileDialog ofd = new OpenFileDialog(); 50 ofd.InitialDirectory = @"C:\Users\jjw\Desktop\图片\video"; 51 ofd.Filter = "所有文件|*.*|视频文件|*.mp4"; 52 ofd.Multiselect = true; 53 ofd.ShowDialog(); 54 55 string[] path = ofd.FileNames; 56 for (int i = 0; i < path.Length; i++) 57 { 58 listBox1.Items.Add(Path.GetFileName(path[i])); 59 list.Add(path[i]); 60 } 61 } 62 63 private void listBox1_DoubleClick(object sender, EventArgs e) 64 { 65 if (listBox1.SelectedIndex == -1) 66 { 67 MessageBox.Show("没哟歌曲"); 68 return; 69 } 70 musicPlayer.URL = list[listBox1.SelectedIndex]; 71 musicPlayer.Ctlcontrols.play(); 72 } 73 } 74 }