using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; namespace XCGAMVPlayer { public partial class Form1 : Form { public Form1() { InitializeComponent(); } List<string> listSong = new List<string>(); private void button1_Click(object sender, EventArgs e) { OpenFileDialog open = new OpenFileDialog(); open.Title = "请选择要播放的文件"; open.InitialDirectory = @"E:\"; open.Multiselect = true; open.Filter = "所有文件|*.*|音乐文件|*.mp3|音乐文件|*.wav"; open.ShowDialog(); string[] path = open.FileNames; for (int i = 0; i < path.Length; i++) { listBox1.Items.Add(Path.GetFileName(path[i])); listSong.Add(path[i]); } } private void listBox1_DoubleClick(object sender, EventArgs e) { axWindowsMediaPlayer1.URL=listSong[listBox1.SelectedIndex]; } private void btnUp_Click(object sender, EventArgs e) { int index = listBox1.SelectedIndex; index--; if (index < 0) { index = listBox1.Items.Count - 1; } listBox1.SelectedIndex = index; axWindowsMediaPlayer1.URL = listSong[index]; } private void btnNext_Click(object sender, EventArgs e) { int index = listBox1.SelectedIndex; index++; if (index == listBox1.Items.Count) { index = 0; } listBox1.SelectedIndex = index; axWindowsMediaPlayer1.URL = listSong[index]; } private void axWindowsMediaPlayer1_StatusChange(object sender, EventArgs e) { //利用播放器状态实现自动播放下一首存在BUG,留待日后灵光一现 //int index =listBox1.SelectedIndex; //if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsStopped) //{ // //index=index+1; // // listBox1.SelectedIndex = index; // //axWindowsMediaPlayer1.URL = listSong[index]; //} } private void Form1_Load(object sender, EventArgs e) { Timer timer1 = new Timer(); timer1.Tick +=new EventHandler( Timer1_Tick); timer1.Start(); timer1.Interval = 10; } private void Timer1_Tick(object sender, EventArgs e) { int index = listBox1.SelectedIndex; if ((int)axWindowsMediaPlayer1.playState == 1) { if (listBox1.SelectedIndex < listBox1.Items.Count - 1) { listBox1.SelectedIndex = index + 1; axWindowsMediaPlayer1.URL = listSong[index + 1]; } else if (listBox1.Items.Count > 0) { index = 0; listBox1.SelectedIndex = 0; axWindowsMediaPlayer1.URL = listSong[index]; listBox1.SelectedIndex = index; axWindowsMediaPlayer1.URL = listSong[index]; } } } //设定一个布尔类型,用来分辨歌曲播放后点击暂停再次点击播放时可以继续播放 而不是从头播放 bool b = true; private void btnPlay_Click(object sender, EventArgs e) { try { if (btnPlay.Text == "播放") { if (b) { //获得选中的歌曲 并从头播放 axWindowsMediaPlayer1.URL = listSong[listBox1.SelectedIndex]; } axWindowsMediaPlayer1.Ctlcontrols.play(); btnPlay.Text = "暂停"; } else if (btnPlay.Text == "暂停") { axWindowsMediaPlayer1.Ctlcontrols.pause(); btnPlay.Text = "播放"; b = false; } } catch { MessageBox.Show("没有任何内容可以播放,施主请不要淘气"); } } /// <summary> /// 实现当切换下标后,更改btnplay的text为播放,点击后,播放当前选中歌曲 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { btnPlay.Text = "播放"; if (btnPlay.Text == "暂停") { axWindowsMediaPlayer1.URL = listSong[listBox1.SelectedIndex]; } } } }
利用timer来实现自动播放下一首歌曲,同时实现播放、暂停的切换显示及对应的功能。应该还是有BUG的,另外使用Install发布后在别的电脑安装运行后有问题,目前还不知道到底是因为什么.......