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; using WMPLib; namespace musicPlayer { public partial class Form1 : Form { //声明一个list存储文件路径 List<string> urlList = new List<string>(); double max, min; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e) { } private void trackBar1_Scroll(object sender, EventArgs e) { } //添加歌曲 private void music_put_Click(object sender, EventArgs e) { //实例化一个打开文件的对话框 OpenFileDialog of = new OpenFileDialog(); //让选择器可以同时选择多个文件 of.Multiselect = true; of.Title = "请选择音乐文件"; //指定文件类型 of.Filter = "(*.mp3)|*.mp3"; //of.ShowDialog();//弹出文件对话框的 if(of.ShowDialog()==DialogResult.OK) { //读取文件 存储到数组 string[] nameList = of.FileNames; //读取数组中的数据 foreach(string url in nameList) { //Path.GetFileNameWithoutExtension(url); listBoxMusics.Items.Add(Path.GetFileNameWithoutExtension(url)); //获取歌曲路径,存在List数组中 urlList.Add(url); } } } //播放音乐 private void play_Click(object sender, EventArgs e) { //看之前有没有选中,有——从选中播出,没有——从0开始播出 int selectedIndex = listBoxMusics.SelectedIndex; //这是为了暂停键做的判断 测试结果:没选时selectedIndex=-1 if (selectedIndex < 0) { selectedIndex = selectedIndex < 0 ? 0 : selectedIndex; //更新选中行 重新设置当前选择行的索引 listBoxMusics.SelectedIndex = selectedIndex; //把获取的索引赋值给播放器控件 axWindowsMediaPlayer1.URL = urlList[selectedIndex]; //移动选中栏到相应位置 musicPlayername.Text = listBoxMusics.SelectedItem.ToString(); } else { axWindowsMediaPlayer1.Ctlcontrols.play(); } timer1.Enabled = true; } //歌曲暂停 private void pause_Click(object sender, EventArgs e) { axWindowsMediaPlayer1.Ctlcontrols.pause(); } //停止播放 private void stop_Click(object sender, EventArgs e) { axWindowsMediaPlayer1.Ctlcontrols.stop(); timer1.Enabled = false;//停止记录,不然跳到下一首歌 } //上一曲 private void last_Click(object sender, EventArgs e) { //上一曲就是当前歌曲的索引减一 int selectedIndex = listBoxMusics.SelectedIndex-1; //假设有三首歌,总数为3 索引为0 1 2 //不循环,一直停在第一首 //selectedIndex = selectedIndex < 0 ? 0 : selectedIndex; //循环 selectedIndex = selectedIndex < 0 ? listBoxMusics.Items.Count-1: selectedIndex; //更新选中行 重新设置当前选择行的索引 listBoxMusics.SelectedIndex = selectedIndex; //把获取的索引赋值给播放器控件 axWindowsMediaPlayer1.URL = urlList[selectedIndex]; //移动选中栏到相应位置 musicPlayername.Text = listBoxMusics.SelectedItem.ToString(); } //下一曲 private void next_Click(object sender, EventArgs e) { //下一曲就是当前歌曲的索引加一 int selectedIndex = listBoxMusics.SelectedIndex +1; //假设有三首歌,总数为3 索引为0 1 2 //不循环,一直停在最后一首 //selectedIndex = selectedIndex <listBoxMusics.Items.Count?selectedIndex:listBoxMusics.Items.Count-1; //循环 selectedIndex = selectedIndex < listBoxMusics.Items.Count ? selectedIndex :0; //更新选中行 重新设置当前选择行的索引 listBoxMusics.SelectedIndex = selectedIndex; //把获取的索引赋值给播放器控件 axWindowsMediaPlayer1.URL = urlList[selectedIndex]; //移动选中栏到相应位置 musicPlayername.Text = listBoxMusics.SelectedItem.ToString(); } //列表发生变化选择播放的歌曲 private void listBoxMusics_SelectedIndexChanged(object sender, EventArgs e) { //获取当前选中歌曲索引 int selectedIndex = listBoxMusics.SelectedIndex; //把获取的索引赋值给播放器控件 axWindowsMediaPlayer1.URL = urlList[selectedIndex]; } private void axWindowsMediaPlayer1_Enter(object sender, EventArgs e) { } private void musicPlayername_Click(object sender, EventArgs e) { } //鼠标按下 private void trackBar1_MouseDown(object sender, MouseEventArgs e) { //鼠标按下时,获取播放时的位置 timer1.Enabled = false; axWindowsMediaPlayer1.Ctlcontrols.pause(); } //鼠标抬起 private void trackBar1_MouseUp(object sender, MouseEventArgs e) { //获取被拖动以后的位置 double doValue = trackBar1.Value; //重置播放位置 axWindowsMediaPlayer1.Ctlcontrols.currentPosition = doValue; axWindowsMediaPlayer1.Ctlcontrols.play(); timer1.Enabled = true; } private void timer1_Tick(object sender, EventArgs e) { //获取文件长度 时间长度 max = axWindowsMediaPlayer1.currentMedia.duration; //获取当前歌曲播放位置 min = axWindowsMediaPlayer1.Ctlcontrols.currentPosition; //类型强转 trackBar1.Maximum = (int)(max); trackBar1.Value = (int)(min); //一首歌播放完,播放下一首 if(axWindowsMediaPlayer1.playState==WMPPlayState.wmppsStopped) { int selectedIndex = listBoxMusics.SelectedIndex + 1; //不循环 //selectedIndex = selectedIndex < listBoxMusics.Items.Count ? selectedIndex : listBoxMusics.Items.Count - 1; //循环 selectedIndex = selectedIndex < listBoxMusics.Items.Count ? selectedIndex : 0; axWindowsMediaPlayer1.URL = urlList[selectedIndex]; listBoxMusics.SelectedIndex = selectedIndex;//选中 musicPlayername.Text = listBoxMusics.SelectedItem.ToString(); trackBar1.Value = 0; timer1.Enabled = true; } } } }
自看随笔,直接赋值用不了。
环境:vs2019
C# winfrom
标签:播放器,sender,void,音乐,System,private,selectedIndex,using From: https://www.cnblogs.com/qingfeng66/p/17369179.html