C# 多媒体播放器
//停止播放 public void stopFile() { axWindowsMediaPlayer1.Ctlcontrols.stop(); } //暂停文件 public void pauseFile() { axWindowsMediaPlayer1.Ctlcontrols.pause(); } //播放文件 public void starFile() { axWindowsMediaPlayer1.Ctlcontrols.play(); } //列表清空 public void clearFile() { listBox1.Items.Clear(); axWindowsMediaPlayer1.Ctlcontrols.stop(); } private void 全屏ToolStripMenuItem_Click(object sender, EventArgs e) { if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsPlaying) { axWindowsMediaPlayer1.fullScreen = !axWindowsMediaPlayer1.fullScreen; } } private String file = ""; List<string> list = new List<string>(); //删除单个文件 public void delFile() { for (int i = 0; i < listBox1.Items.Count; i++) { if (listBox1.SelectedItems.Contains(listBox1.Items[i])) { listBox1.Items.Remove(listBox1.Items[i]); axWindowsMediaPlayer1.Ctlcontrols.stop(); } } } private void 打开ToolStripMenuItem_Click(object sender, EventArgs e) { DialogResult dr = openFileDialog1.ShowDialog(); openFileDialog1.Multiselect = true; switch (dr) { case DialogResult.OK: file = openFileDialog1.FileName; list.Add(file); axWindowsMediaPlayer1.URL = openFileDialog1.FileName; listBox1.Items.Add( openFileDialog1.FileName.Substring(file.LastIndexOf("\\")+1)); break; case DialogResult.No: break; } } private void button5_Click(object sender, EventArgs e) { starFile(); } private void button6_Click(object sender, EventArgs e) { pauseFile(); } private void button7_Click(object sender, EventArgs e) { stopFile(); } //上一曲 private void button3_Click(object sender, EventArgs e) { for (int i = 0; i < listBox1.Items.Count; i++) { if (listBox1.SelectedItems.Contains(listBox1.Items[i])) { //如果是顶部 if (listBox1.SelectedIndex == 0) { MessageBox.Show("已经达到顶部"); } //否则就上一曲 else { listBox1.SetSelected(listBox1.SelectedIndex - 1, true); axWindowsMediaPlayer1.URL = list[listBox1.SelectedIndex]; return; } } } } //下一曲 private void button4_Click(object sender, EventArgs e) { for (int i = 0; i < listBox1.Items.Count; i++) { if (listBox1.SelectedItems.Contains(listBox1.Items[i])) { //如果是顶部 if (listBox1.SelectedIndex == listBox1.Items.Count - 1) { MessageBox.Show("已经达到尾部部"); //MessageBox.Show(listBox1.Items.Count.ToString()); } //否则就下一曲 else { listBox1.SetSelected(listBox1.SelectedIndex+1, true); //让歌曲切换到下一曲 axWindowsMediaPlayer1.URL = list[listBox1.SelectedIndex]; return; } } } } private void 全选列表ToolStripMenuItem_Click(object sender, EventArgs e) { listBox1.MultiColumn = true; //listBox1.SetSelected(0, true); //listBox1.SetSelected(1, true); //listBox1.SetSelected(2, true); listBox1.Select(); } private void 删除ToolStripMenuItem_Click(object sender, EventArgs e) { delFile(); } private void 播放ToolStripMenuItem_Click(object sender, EventArgs e) { starFile(); } private void 停止ToolStripMenuItem_Click(object sender, EventArgs e) { stopFile(); } private void 暂停ToolStripMenuItem_Click(object sender, EventArgs e) { pauseFile(); } private void 快退ToolStripMenuItem_Click(object sender, EventArgs e) { axWindowsMediaPlayer1.Ctlcontrols.fastReverse(); } private void 快进ToolStripMenuItem_Click(object sender, EventArgs e) { axWindowsMediaPlayer1.Ctlcontrols.fastForward(); } private void 清空ToolStripMenuItem_Click(object sender, EventArgs e) { delFile(); }
初入江湖程序猿