C#、.NET迷你音乐播放器
闲来没事自己做了一个基于WindowsMediaPlayer的迷你音乐播放器,界面如下图
功能简介:
1、循环模式:顺不播放
axWMusicPlayer.settings.setMode("shuffle", false);
全部循环
axWMusicPlayer.settings.setMode("loop", true);
随机播放
axWMusicPlayer.settings.setMode("shuffle", true);
2、模拟定时关机
程序写到定时关机,具体的调用定时关机程序省略了,程序中相应地方有注解。
1 private void 关机时间toolStripTextBox_KeyPress(object sender, KeyPressEventArgs e) 2 { 3 if (e.KeyChar == (char)Keys.Enter && 关机时间toolStripTextBox.Text != "") 4 { 5 try 6 { 7 DateTime time = DateTime.Parse(关机时间toolStripTextBox.Text); 8 TimeSpan span = new TimeSpan(time.Hour, time.Minute, 0); 9 if (span.CompareTo(new TimeSpan(DateTime.Now.Hour, DateTime.Now.Minute, 0)) == 0) //当前关机时间10 {11 if (MessageBox.Show("你设定的关机时间是当前计算机时间,是否直接关机?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.OK)12 {13 MynotifyIcon.Icon = Icon.ExtractAssociatedIcon(PathBase + "\\Images\\" + "ShutDown_notifyIcon.ico");14 //15 //调用关机程序16 //17 }18 else19 {20 return;21 }22 }23 if (span.CompareTo(new TimeSpan(DateTime.Now.Hour, DateTime.Now.Minute, 0)) < 0) //过去关机时间24 {25 MessageBox.Show("此时间已是过去时间,设定无效", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);26 return;27 }28 if (span.CompareTo(new TimeSpan(DateTime.Now.Hour, DateTime.Now.Minute, 0)) > 0) //有效关机时间29 {30 31 ShutDownTime = 关机时间toolStripTextBox.Text;32 MessageBox.Show("成功设置定时关机,计算机将于“" + ShutDownTime + "”关机…… *︶︵︶*", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);33 this.关机时间toolStripTextBox.Visible = false;34 this.取消定时关机QToolStripMenuItem.Visible = true;35 36 //37 //调用关机程序38 //39 }40 }41 catch42 {43 MessageBox.Show("日期格式不正确,请重新输入。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);44 return;45 }46 }47 if (e.KeyChar == (char)Keys.Enter && 关机时间toolStripTextBox.Text == "")48 {49 关机时间toolStripTextBox.Visible = false;50 }51 }
3、歌曲列表信息保存在XML文件中
1 private void btnList_Click(object sender, EventArgs e) //添加歌曲 2 { 3 openFileDialog1.Title = "添加歌曲"; 4 openFileDialog1.FileName = ""; 5 openFileDialog1.Multiselect = true; 6 openFileDialog1.Filter = "Mp3文件|*.mp3|Wav文件|*.wav|Wma文件|*.wma|Wmv文件|*.wmv|所有格式|*.*"; 7 openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyMusic); 8 try 9 {10 if (openFileDialog1.ShowDialog() == DialogResult.OK)11 {12 //IsCreateXmlFile();13 XmlDocument xmldoc = new XmlDocument();14 xmldoc.Load(xmlfile);15 XmlNode root = xmldoc.SelectSingleNode("MusicList");16 string[] FileNamesList = openFileDialog1.FileNames;17 foreach (string file in FileNamesList)18 {19 string filename= Path.GetFileName(file).Substring(0,Path.GetFileName(file).LastIndexOf('.'));20 axWMusicPlayer.currentPlaylist.appendItem(axWMusicPlayer.newMedia(file));21 XmlElement newelement = xmldoc.CreateElement("MusicProperty");22 newelement.SetAttribute("MusicUrl", file);23 newelement.SetAttribute("MusicName", filename);24 newelement.SetAttribute("LikeCount", "0");25 root.AppendChild(newelement);26 Application.DoEvents();27 }28 xmldoc.Save(xmlfile);29 }30 }31 catch(Exception ex)32 {33 MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);34 }35 }
4、主界面上的喜爱按钮实现的是歌曲排列顺序
在下次启动程序后,程序会自动将喜爱点击次数多的歌曲排到前面
1 private void btnFavorite_Click(object sender, EventArgs e) //喜爱 2 { 3 if (axWMusicPlayer.currentMedia != null) 4 { 5 XmlDocument doc = new XmlDocument(); 6 doc.Load(xmlfile); 7 XmlNodeList nodelist = doc.SelectSingleNode("MusicList").ChildNodes; 8 foreach (XmlNode node in nodelist) 9 {10 XmlElement element = (XmlElement)node; //将XmlNode节点node转化成XmlElement型的node11 if (element.GetAttribute("MusicUrl") == axWMusicPlayer.currentMedia.sourceURL)12 {13 int likecount = Convert.ToInt32(element.GetAttribute("LikeCount")) + 1;14 element.SetAttribute("LikeCount", likecount.ToString());15 }16 }17 doc.Save(xmlfile);18 }19 }
5、点击进度条歌曲直接跳到当前位置继续播放
设置当前播放位置
private void pictureBox2_MouseDown(object sender, MouseEventArgs e){if (e.Button == MouseButtons.Left){axWMusicPlayer.Ctlcontrols.currentPosition = e.X * axWMusicPlayer.currentMedia.duration / 170;}}
注解:170为进度条总长度
6、进度条采用自定义控件
1 /// <summary> 2 /// 迷你音乐播放器 3 /// <author> 4 ///<name>漓江烟雨</name> 5 ///<QQ>648445213</QQ> 6 ///<date>2011.12.3</date> 7 /// </author> 8 /// </summary> 9 10 11 namespace UCCurrentTime12 {13 public partial class UserControl1 : UserControl14 {15 /// <summary>16 /// 设置或获取当前播放进度条的宽度17 /// </summary>18 public int UCTime19 {20 get { return this.Width; }21 set 22 {23 if (this.Width >= 16)24 {25 this.Width = value;26 }27 else28 {29 this.Width = value + 16;30 }31 32 }33 }34 35 /// <summary>36 /// 设置或获取当前播放进度条左边背景图片37 /// </summary>38 public Image LeftBackImage39 {40 get { return picBoxLeft.BackgroundImage; }41 set { picBoxLeft.BackgroundImage = value; } 42 }43 44 /// <summary>45 /// 设置或获取当前播放进度条右边背景图片46 /// </summary>47 public Image RightBackImage 48 {49 get { return picBoxRight.BackgroundImage; }50 set { picBoxRight.BackgroundImage = value; } 51 }52 53 /// <summary>54 /// 设置或获取当前播放进度条中间背景图片55 /// </summary>56 public Image MiddleBackImage 57 {58 get { return picBoxMiddle.BackgroundImage; }59 set { picBoxMiddle.BackgroundImage = value; } 60 }61 public UserControl1()62 {63 InitializeComponent();64 65 picBoxLeft.Size = new Size(8, 10); //左边66 picBoxRight.Size = new Size(8, 10); //右边67 picBoxMiddle.Size = new Size(0, 10); //中间68 69 picBoxLeft.Dock = DockStyle.Left;70 picBoxRight.Dock = DockStyle.Right;71 picBoxMiddle.Width = this.Width - 16;72 picBoxMiddle.Location = new Point(8, 0);73 74 this.BackColor = Color.Transparent;75 picBoxLeft.BackColor = Color.Transparent;76 picBoxRight.BackColor = Color.Transparent;77 picBoxMiddle.BackColor = Color.Transparent;78 79 picBoxLeft.BackgroundImage = LeftBackImage;80 picBoxRight.BackgroundImage = RightBackImage;81 picBoxMiddle.BackgroundImage = MiddleBackImage;82 }83 84 private void UserControl1_SizeChanged(object sender, EventArgs e)85 {86 this.Size = new Size(UCTime, 10);87 picBoxMiddle.Width = this.Width - 16;88 }89 }90 }
timer_tick事件
1 private void timer1_Tick(object sender, EventArgs e) 2 { 3 try 4 { 5 if (axWMusicPlayer.currentMedia != null) 6 { 7 if (axWMusicPlayer.currentMedia.name.Length > 9) 8 { 9 lblMusicName.Text = axWMusicPlayer.currentMedia.name.Substring(0, 9) + "...";10 }11 else12 {13 lblMusicName.Text = axWMusicPlayer.currentMedia.name;14 }15 lblcurrenttime.Text = axWMusicPlayer.Ctlcontrols.currentPositionString + " / " + axWMusicPlayer.currentMedia.durationString;16 UCcurrenttime = Convert.ToInt32(axWMusicPlayer.Ctlcontrols.currentPosition / axWMusicPlayer.currentMedia.duration * 170);17 if (UCcurrenttime <= 16)18 {19 UCcurrenttime = 16;20 }21 this.userControl11.Width = UCcurrenttime;22 }23 }24 catch25 {26 return;27 }28 }
this.userControl11.Width = UCcurrenttime; 设置进度条长度
7、由于去掉了默认的标题框所以自己添加了鼠标拖动事件
private void MiniMusicPlayer_MouseDown(object sender, MouseEventArgs e) {if (e.Button == MouseButtons.Left) { FormPositioX = e.X; FormPositioY = e.Y; } } private void MiniMusicPlayer_MouseMove(object sender, MouseEventArgs e) {if (e.Button == MouseButtons.Left) {this.Left += e.X - FormPositioX;this.Top += e.Y - FormPositioY; } }
8、检测窗体位置,超出了屏幕范围会自动靠边
1 private void MiniMusicPlayer_MouseUp(object sender, MouseEventArgs e) 2 { 3 if (this.Left <= 0) 4 { 5 this.Left = 0; 6 } 7 if (this.Top <= 0) 8 { 9 this.Top = 0;10 }11 12 int ScreenEidth = Screen.GetWorkingArea(this).Width; //获得屏幕的宽13 int ScreenGeight = Screen.GetWorkingArea(this).Height; //获得屏幕的高14 if (this.Left + this.Width >= ScreenEidth)15 {16 this.Left = ScreenEidth - this.Width;17 }18 if (this.Top + this.Height >= ScreenGeight)19 {20 this.Top = ScreenGeight - this.Height;21 }22 }
9、组合键控制音量,默认音量为70
1 private void MiniMusicPlayer_KeyDown(object sender, KeyEventArgs e) //接收组合键调节音量 2 { 3 if ((e.Control) && (e.Alt) && (e.KeyCode == Keys.Up)) 4 { 5 if (axWMusicPlayer.settings.volume >= 100) 6 { 7 axWMusicPlayer.settings.volume = 100; 8 } 9 else10 {11 axWMusicPlayer.settings.volume += 5;12 }13 }14 if (e.Control && e.Alt && (e.KeyCode == Keys.Down))15 {16 if (axWMusicPlayer.settings.volume <= 0)17 {18 axWMusicPlayer.settings.volume = 0;19 }20 else21 {22 axWMusicPlayer.settings.volume -= 5;23 }24 }25 }
10、跟据点击喜爱次数填充播放列表
1 /// <summary> 2 /// 填充播放列表 3 /// </summary> 4 protected void FillCurrenrList() 5 { 6 XmlDocument doc = new XmlDocument(); 7 doc.Load(xmlfile); 8 if (doc.SelectSingleNode("MusicList").ChildNodes.Count != 0) 9 {10 XmlNodeList NodeList = doc.SelectSingleNode("MusicList").ChildNodes;11 string[] musiclist = new string[doc.SelectSingleNode("MusicList").ChildNodes.Count]; //每条MusicProperty组成一维数组12 int num = 0;13 foreach (XmlNode node in NodeList)14 {15 XmlElement element = (XmlElement)node;16 musiclist[num] = element.GetAttribute("MusicUrl") + ";" + element.GetAttribute("LikeCount");17 num++;18 }19 20 string[,] musiclist2 = new string[num, 2]; //将一维数组分割为二维数组:MusicUrl、LikeCount21 for (int i = 0; i < num; i++)22 {23 string[] hh = musiclist[i].Split(';');24 musiclist2[i, 0] = hh[0];25 musiclist2[i, 1] = hh[1];26 }27 28 string temp;29 for (int i = 0; i < num - 1; i++) //按点击次数冒泡排序,排序后的数组添加到axWMusicPlayer.currentPlaylist30 {31 for (int j = 0; j < num - 1 - i; j++)32 {33 if (Convert.ToInt32(musiclist2[j, 1]) < Convert.ToInt32(musiclist2[j + 1, 1]))34 {35 temp = musiclist2[j, 1];36 musiclist2[j, 1] = musiclist2[j + 1, 1];37 musiclist2[j + 1, 1] = temp;38 39 temp = musiclist2[j, 0];40 musiclist2[j, 0] = musiclist2[j + 1, 0];41 musiclist2[j + 1, 0] = temp;42 }43 }44 }45 46 for (int i = 0; i < num; i++) //填充播放列表47 {48 axWMusicPlayer.currentPlaylist.appendItem(axWMusicPlayer.newMedia(musiclist2[i, 0]));49 }50 }51 }
帮助窗口中的一些说明:
VS2008版:
源代码下载地址
VS2010版:
源代码下载地址
申明一下:本人现在不从事软件开发了,所以加我QQ的就免了吧,很久没碰了,现在都忘了,非常抱歉没能帮到大家……(2013.3.2)
转载于:https://www.cnblogs.com/lijiangyanyu/archive/2011/12/08/2280582.html
C#、.NET迷你音乐播放器相关推荐
- 用js创建audio对象实现网页迷你音乐播放器
主要是靠咋没的audio对象,我就不多说废话了,也不会说,直接上代码: HTML <!DOCTYPE html> <html><head><meta http ...
- 迷你音乐播放器v1.0正式上线!
迷你音乐播放器V1.0正式上线! 版本介绍: 1.随机播放切换开关(通过点击专辑图片) 2.通过拖动歌曲名及艺术家名调整歌曲播放进度 3.手机浏览访问支持熄屏播放 4.暂不支持在线搜索功能 快来一起分 ...
- 个人小作品之迷你音乐播放器(移动端)
关于viewport viewport是用户网页的可视区域,中文可称为"可视区". 手机浏览器是将页面放在一个虚拟的窗口(viewport)中,通常这个窗口比屏幕宽,这样就不用把每 ...
- Flash版(迷你音乐播放器mp3Player1.0 Beta1 )附源码
迷你音乐播放器mp3Player1.0 Beta1 ================= //What's new: 主要功能特色: 1.支持歌曲名称,歌手名称以及专辑等关键字搜索. 2.支持随机搜索 ...
- html迷你音乐播放器,javascript开发迷你音乐播放器
web前端群,189394454,有视频.源码.学习方法等大量干货分享 知识点:html/css布局思维,音频标签api运用,css3自定义动画,Js音乐播放控制,歌词同步等.
- iOS开发--手把手教你制作音乐播放器
我个人之前有个想法,把平常用的小软件,闲暇之余自己都实现一个迷你版本,打造自己的app专属文件夹,比如闹钟.音乐播放器.视频播放器.电子书以及贪吃蛇之类的小游戏.我相信通过实现这些小程序,能够进一步熟 ...
- 几款最主流的音乐播放器应用源码-android源码
影音播放 优雅音乐播放器应用源码 这是一款不错的音乐播放器应用源码案例,优雅音乐播放器应用源码,该应用的界面有点类似小米音乐播放器,应用的整天布局还没有那 人气:3328运行环境:/android/i ...
- 良心推荐:高品质音乐播放器Audirvana for Mac
今天小编给大家推荐一款Mac平台的高品质无损音乐播放器Audirvana for Mac!!它是一个基于原生支援无损音讯压缩编码FLAC(Free Lossless Audio Codec)的音乐播放 ...
- 简洁的 HTML5 音乐播放器
mePlayer 一款简洁的HTML5音乐播放器.内置两套主题,可在运行时一键切换, 点我在线预览 默认主题 有歌词时滚动显示歌词,无歌词时显示模拟频谱动画 播放中鼠标悬停在界面上滑动滚轮可调节音量, ...
最新文章
- LINUX内核分析第八周总结:进程的切换和系统的一般执行过程
- 如何避免订单重复支付?
- 连续三年蝉联第一,Flink 荣膺全球最活跃的 Apache 开源项目
- 欧拉回路 欧拉路径
- Spring Cloud微服务之业务物理删除(十)
- 卸任后的马云“忙”坏了,健美大赛、蹦迪、修空调、种蘑菇,真相看懵了
- 玩转Excel系列-SUMIF函数实例教程
- jwt 长度_跟我一起学.NetCore之WebApi接口裸奔有风险(Jwt)
- 2022 SpringBoot/SSM的药品售货机平台 H5药品购买商城
- C语言表白程序系列(不接受表白就让你电脑关机)
- python3 mysql插入数据冲突
- Python实现高级电影特效,CXK也能影分身
- Linux防火墙配置工具iptables中MASQUERADE的含义
- android源码编译烧鸡,板栗烧鸡的做法图文
- 嘀嘀的费用是优步的两倍?
- kaldi-关键词识别kws
- 前端 day01 常用基础语法
- 强化学习入坑之路04
- 【math】Hiden Markov Model 隐马尔可夫模型了解
- Exchange ActiveSync 配置
热门文章
- 第五届中国网络安全大会(NSC2017)
- 为什么28岁不能学java_28岁大龄人学java不是不可能
- Ubuntu18.04无法连接Wifi解决方案
- 单工通信/半双工通信/全双工通信,串行传输/并行传输,同步传输/异步传输的区别
- 青春是一场不失不忘的期许
- Boost.Geometry中的几何要素(Primitives)
- 找工作必看!!!关于什么叫做三方协议
- JS 轮播图 图片切换(定时器)
- 如何防止恶意扫码点餐
- 建行u盾单片机可以再次使用吗_Si7021建行U盾19264液晶制作温湿度显示,实物单片机代码开源...