C# Winform小项目——音乐播放器

单记录控件很容易忘,使用才能记得牢固,这边就通过一个音乐播放器的小项目来巩固自己的Winform学习

 这个音乐播放器用到了以下控件:

Label 标签,Button按钮、ListBox列表框、Timer控件、TableLayoutPanel控件、TracBar控件以及自定义控件 AxWindowsMediaPlayer。

Label、Button、ListBox以及TableLayoutPanel这几个都是基础控件,就不细谈

Timer控件:

主要作用:存在于背景进程中。Timer 控件可以定时的循环执行代码。
值得强调的是,Timer不是基于异步或者多线程执行代码的。也就是说这个空间在执行的时候是占用UI线程的。
如果Timer里的代码块运行时长不确定,可能会超出所设置的时长,可以在Timer开始执行的时候关闭Timer控件,再在相应的程序结束后打开Timer控件。这个可以通过设置Timer的Enabled属性来实现。

如果你Timer控件中的程序访问了UI线程的控件,可能会出现UI的控件还没加载完毕就访问的错误。这时候可以将Timer控件的调用放到,界面加载完毕的函数中,或者其他类似函数。如果有人问的话再细致讲。

选择拖到窗体中,下方会自动出现控件
主要属性:
Enable:当前Timer控件是否可用。
timer1.Enable = false;//不可用
timer1.Enable = true;//可用
Interval 设置事件执行的间隔时间

Tick事件:每经过一定的时间自动执行一次

应用实例:在开发中,需要定时上传设备信息

参考文章:点这里

 

TracBar控件:

进度条,设置最大值最小值

重要事件:Scroll事件、ValueChanged事件

Value为int类型

 

自定义控件AxWindowsMediaPlayer 是引入的。

 

文件过滤器:Filter方法,参数为字符串,字符串的格式为:“名称|*.需要筛选的文件后缀;”  //加分号。

OpenFileDialog ofd = new OpenFileDialog();

示例:ofd.Filter = "图片|*.gif;*.jpg;*.jpeg;*.bmp;*.jfif;*.png;";//限制只能选择这几种图片格式

如果有多种同类型文件(比如都是图片),名称只需要写一个,后缀用分号(;)分隔。

示例:ofd.Filter ="图片|*.gif;*.jpg;*.jpeg;*.bmp;*.jfif;*.png;|文档|*.doc;|Excel|*.xls;*.xlsx";

 

主要逻辑代码:

  1     public partial class Form1 : Form
  2     {
  3         //存储文件的路径
  4         List<string> urlList = new List<string>();
  5         double max, min;
  6         int lastIndex = -1;
  7         public Form1()
  8         {
  9             InitializeComponent();
 10         }
 11         // 添加歌曲
 12         private void btnInput_Click(object sender, EventArgs e)
 13         {
 14             //实例化一个打开文件的对话框
 15             OpenFileDialog ofd = new OpenFileDialog();
 16             //让选择其可以同时选择多个文件
 17             ofd.Multiselect = true;
 18             ofd.Title = "请选择音乐文件";
 19             //指定选择文件的类型
 20             ofd.Filter = "(*.mp3)|*.mp3";
 21             //string str = "文本文件|*.txt;*.html;*.docx;*.doc|图像文件|*.jpg;*.jpeg";
 22             //确认用户选择确定
 23             if (ofd.ShowDialog() == DialogResult.OK)
 24             {
 25                 //把用户选择的文件存储到数组中
 26                 string[] nameList = ofd.FileNames;
 27                 //读取数组中的数据
 28                 foreach (string url in nameList)
 29                 {
 30                     //Path.GetFileNameWithoutExtension 获取不具有扩展名的文件名
 31                     listBoxMusic.Items.Add(Path.GetFileNameWithoutExtension(url));
 32                     urlList.Add(url);
 33                 }
 34             }
 35         }
 36         private void btnPlay_Click(object sender, EventArgs e)
 37         {
 38             int selectedIndex;
 39             //判断列表中是否有选择的歌曲,有的话,播放选中的,没有的话,播放第一首
 40             selectedIndex = listBoxMusic.SelectedIndex == -1 ? 0 : listBoxMusic.SelectedIndex;
 41             if (labelMusicName.Text == "暂无播放音乐" || selectedIndex < 0)
 42             {
 43                 //ListBox中的索引和urlList中的索引相对应
 44                 //获取当前选中歌曲的索引
 45                 selectedIndex = selectedIndex < 0 ? listBoxMusic.Items.Count - 1 : selectedIndex;
 46                 //更新选择行
 47                 listBoxMusic.SelectedIndex = selectedIndex;
 48                 //把urlList中存储的url地址赋给播放器控件
 49                 axWindowsMediaPlayer1.URL = urlList[selectedIndex];
 50                 labelMusicName.Text = listBoxMusic.SelectedItem.ToString();
 51                 lastIndex = listBoxMusic.SelectedIndex;
 52             }
 53             else
 54             {
 55                 axWindowsMediaPlayer1.Ctlcontrols.play();
 56             }
 57             timer1.Enabled = true;
 58         }
 59         //暂停
 60         private void btnPause_Click(object sender, EventArgs e)
 61         {
 62             axWindowsMediaPlayer1.Ctlcontrols.pause();
 63         }
 64         //停止
 65         private void btnStop_Click(object sender, EventArgs e)
 66         {
 67             trackBar1.Value = 0;
 68             timer1.Enabled = false;
 69             axWindowsMediaPlayer1.Ctlcontrols.stop();
 70             lastIndex = listBoxMusic.SelectedIndex;
 71         }
 72         //上一首
 73         private void btnPrevious_Click(object sender, EventArgs e)
 74         {
 75             //ListBox中的索引和urlList中的索引相对应
 76             //获取当前选中歌曲的索引
 77             int selectedIndex = listBoxMusic.SelectedIndex - 1;
 78             selectedIndex = selectedIndex < 0 ? listBoxMusic.Items.Count-1 : selectedIndex;
 79             //更新选择行
 80             listBoxMusic.SelectedIndex = selectedIndex;
 81             //把urlList中存储的url地址赋给播放器控件
 82             axWindowsMediaPlayer1.URL = urlList[selectedIndex];
 83             labelMusicName.Text = listBoxMusic.SelectedItem.ToString();
 84             lastIndex = listBoxMusic.SelectedIndex;
 85         }
 86 
 87         private void btnNext_Click(object sender, EventArgs e)
 88         {
 89             //ListBox中的索引和urlList中的索引相对应
 90             //获取当前选中歌曲的索引
 91             int selectedIndex = listBoxMusic.SelectedIndex + 1;
 92             selectedIndex = selectedIndex == listBoxMusic.Items.Count ? 0  : selectedIndex;
 93             //更新选择行
 94             listBoxMusic.SelectedIndex = selectedIndex;
 95             //把urlList中存储的url地址赋给播放器控件
 96             axWindowsMediaPlayer1.URL = urlList[selectedIndex];
 97             labelMusicName.Text = listBoxMusic.SelectedItem.ToString();
 98             lastIndex = listBoxMusic.SelectedIndex;
 99         }
100         //列表选择发生变化时,播放选中的歌曲
101         private void listBoxMusic_SelectedIndexChanged(object sender, EventArgs e)
102         {
103             //ListBox中的索引和urlList中的索引相对应
104             //获取当前选中歌曲的索引
105             int selectedIndex = listBoxMusic.SelectedIndex;
106             if (selectedIndex<0||selectedIndex>=listBoxMusic.Items.Count) return;
107             if (selectedIndex == lastIndex) return;
108 
109             //把urlList中存储的url地址赋给播放器控件
110             axWindowsMediaPlayer1.URL = urlList[selectedIndex];
111             labelMusicName.Text = listBoxMusic.SelectedItem.ToString();
112                 timer1.Enabled = true;
113             lastIndex = listBoxMusic.SelectedIndex;
114         }
115         //鼠标按下
116         private void trackBar1_MouseDown(object sender, MouseEventArgs e)
117         {
118             //鼠标按下时,获取播放时的位置
119             timer1.Enabled = false;
120             axWindowsMediaPlayer1.Ctlcontrols.pause();
121         }
122         //鼠标抬起
123         private void trackBar1_MouseUp(object sender, MouseEventArgs e)
124         {
125             if (listBoxMusic.SelectedIndex == -1) return;
126             //获取拖动的位置
127             axWindowsMediaPlayer1.Ctlcontrols.currentPosition = trackBar1.Value;
128             axWindowsMediaPlayer1.Ctlcontrols.play();
129             timer1.Enabled = true;
130         }
131         //进度条
132         private void timer1_Tick(object sender, EventArgs e)
133         {
134             //获取文件的长度 事件长度
135             max = axWindowsMediaPlayer1.currentMedia.duration;
136             //获取当前歌曲的播放位置
137             min = axWindowsMediaPlayer1.Ctlcontrols.currentPosition;
138 
139             trackBar1.Maximum = (int)max;
140             trackBar1.Value = (int)min;
141             //一首歌播放完成后,播放下一首
142             if (axWindowsMediaPlayer1.playState == WMPPlayState.wmppsStopped)
143             {
144                 int selectedIndex = (listBoxMusic.SelectedIndex + 1)%listBoxMusic.Items.Count;
145                 axWindowsMediaPlayer1.URL = urlList[selectedIndex];
146                 listBoxMusic.SelectedIndex = selectedIndex;
147                 labelMusicName.Text = listBoxMusic.SelectedItem.ToString();
148                 trackBar1.Value = 0;
149                 timer1.Enabled = true;
150                 lastIndex = listBoxMusic.SelectedIndex;
151             }
152         }
153     }
音乐播放器

开发过程中遇到的问题

点击空白处导致的播放出错:

解:添加了一个全局变量(一般最好就不要用全局变量),记录上一次的歌曲下标,当前下标和之前的下标比较,相同就直接返回,不处理。

其他都是小问题,对控件使用的不熟悉,常用即可解决。
还有很多可以优化的地方,现在难以实现。

posted @ 2022-07-12 21:01  xunzf  阅读(1156)  评论(0)    收藏  举报