C#/音乐播放器/带进度条/歌词滚动、颜色变化/桌面应用程序设计
生活随笔
收集整理的這篇文章主要介紹了
C#/音乐播放器/带进度条/歌词滚动、颜色变化/桌面应用程序设计
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
用基本C#知識實現(xiàn)制作一個音樂播放器
前言
寫這個博客并不是說我的作品多么高級或完美,只是希望能在一些功能方面給你們一些啟發(fā),能幫助到你們做出真正好的程序,這就足夠了
話不多說,讓我們開始吧~~~
截圖示意
總體思路
首先呢,選擇這種無邊框窗口,我們會發(fā)現(xiàn)移動窗口成了問題,所以呢,就需要特意對主窗口寫事件,即按下時,移動時,松開時等各是怎樣,這個應該不難。
···
Point downpoint;Point movepoint;bool ismoving;//實現(xiàn)鼠標控制窗口移動private void Form1_MouseDown(object sender, MouseEventArgs e){downpoint = e.Location;ismoving = true;}private void timermove_Tick(object sender, EventArgs e){}//實現(xiàn)鼠標控制窗口移動(2)private void mainForm_MouseMove(object sender, MouseEventArgs e){if (ismoving == true){movepoint = e.Location;this.Location = new Point(this.Location.X + (movepoint.X - downpoint.X),this.Location.Y + (movepoint.Y - downpoint.Y));}}//鼠標按鍵抬起窗口停止移動private void mainForm_MouseUp(object sender, MouseEventArgs e){ismoving = false;}····
然后就是引入WindowsMediaPlayer后我們需要實現(xiàn)音頻文件資源的打開,這里我設置了文件過濾器,只能打開“.MP3”“.MP4”“.flac”三種文件,同時實現(xiàn)了基本功能,例如點擊播放器的播放(暫停)鍵時圖標的變化、歌曲繼續(xù)、暫停等(所以我們用到一個bool型變量方便判斷)
···
bool isplaying = true;string a;//歌曲總時間(字符串)//點擊播放:加載歌詞private void start_Click(object sender, EventArgs e){if (tick == 0){OpenFileDialog ofd = new OpenFileDialog();ofd.Filter = "mp3音樂|*.mp3|mp4音樂|*.mp4|flac文件|*.flac";//設置文件過濾,使之只能打開.mp3/.mp4/flac三種格式的音頻文件ofd.ShowDialog();string fileName = ofd.FileName;media.URL = fileName;media.Ctlcontrols.play();// start_Click(object sender, EventArgs e);timer2.Enabled = true;}tick = 1;timer1.Enabled = true;Encoding encode = Encoding.GetEncoding("GB2312");FileStream fs = new FileStream("陳一發(fā)兒-童話鎮(zhèn).lrc", FileMode.Open);StreamReader sr = new StreamReader(fs, encode);string s = sr.ReadLine();//int xPos = 100;int yPos = 50;string a= media.currentMedia.durationString;stime.Text = a;while (s != null){s = sr.ReadLine();if(s==null){break;}if (s.Equals("")){continue;}Lyric1 lyric =new Lyric1();lyric.minute =int.Parse(s.Substring(1,2));lyric.second = int.Parse(s.Substring(4,2));lyric.lyrics = s.Substring(10);lyric.tosecond = lyric.minute * 60 + lyric.second;//if(s.Substring(10)==null)//{// lyric.sminute = Convert.ToInt32(s.Substring(1, 1));//lyric.ssecond = Convert.ToInt32(s.Substring(5, 1));// }listLyric1.Add(lyric);yPos += 30;} if (isplaying){start.BackgroundImage = Properties.Resources.play_circle_o;isplaying = false;media.Ctlcontrols.pause();timer1.Enabled = false;}else{start.BackgroundImage = Properties.Resources.pause_circle_o;isplaying = true;media.Ctlcontrols.play();timer1.Enabled = true;}fs.Close();sr.Close();}···
另一個功能有一部分也包含在上述代碼中,那就是重要的歌詞繪制(只不過上面主要只實現(xiàn)了歌詞文件(“.lrc”)的讀取),其實有人說用label也能顯示歌詞、甚至實現(xiàn)歌詞滾動、變色,可是吧…效果不太好,一方面不流暢,另一方面顯示效果也差,甚至可能有閃屏
所以我采用了GDI+繪圖實現(xiàn)的
注意哈,GDI+繪圖是不會自動變化的,需要窗口隱藏以至于無效化才會更新
所以我們需要寫程序使需要的區(qū)域(不一定是整個主窗口)不斷自動刷新
···
Graphics g;//繪圖對象int PointY=140;int current;//主窗口繪制歌詞private void mainForm_Paint(object sender, PaintEventArgs e){g = e.Graphics;if (tick == 2){for (int i = 0; i < listLyric.Count; i++){if (i == current)g.DrawString(listLyric[i].lyrics, new Font("微軟雅黑", 17), Brushes.LightBlue, 400, PointY + i * 50);elseg.DrawString(listLyric[i].lyrics, new Font("微軟雅黑", 14), Brushes.White, 400, PointY + i * 50);}}else{for (int i = 0; i < listLyric1.Count; i++){if (i == current)g.DrawString(listLyric1[i].lyrics, new Font("微軟雅黑", 17), Brushes.LightBlue, 400, PointY + i * 50);elseg.DrawString(listLyric1[i].lyrics, new Font("微軟雅黑", 14), Brushes.White, 400, PointY + i * 50);}}}double position;//時鐘:不停調(diào)用刷新主窗口private void timer1_Tick(object sender, EventArgs e){//獲取當前歌曲播放時間position = media.Ctlcontrols.currentPosition;int a = (int)position;int m = a / 60;int s = a % 60;time1.Text = m.ToString();time2.Text = s.ToString();PointY-=1;for (int i = 0; i < listLyric.Count-1; i++){double b = listLyric[i].tosecond;double c = listLyric[i+1].tosecond;// int positioni = (int)position;if (position > b && position < c)current = i;}this.Invalidate(UpdateRect);}Rectangle UpdateRect;//主窗口刷新private void mainForm_Load(object sender, EventArgs e){UpdateRect = new Rectangle(0, 0, this.ClientSize.Width,this.ClientSize.Height - panel1.Height);}···
還有一個功能是包含在上面代碼里的
那就是歌曲總時間與現(xiàn)在時間的顯示,這個的話現(xiàn)在時間直接讀取來的時間通常是以秒為單位,我們?yōu)榱孙@示更加直觀,將其換算為正常的分與秒,總時間就采取直接顯示原來的字符串形式
···
position = media.Ctlcontrols.currentPosition;int a = (int)position;int m = a / 60;int s = a % 60;time1.Text = m.ToString();time2.Text = s.ToString();string a = media.currentMedia.durationString;stime.Text = a;···
最后呢就是進度條怎么來的
這個我用了兩個panel,不同的寬度,不同的顏色,通過在timer里調(diào)用增加藍色panel的長度實現(xiàn),這個我們要用到音頻文件總時間,從而計算出單位timer時間里長度伸展多少,來實現(xiàn)進度條的功能
····
private void timer2_Tick(object sender, EventArgs e){a = media.currentMedia.durationString;int timm = int.Parse(a.Substring(1,1));int tims = int.Parse(a.Substring(3,2));int tim = timm * 60 + tims;sizex += (610/ tim);if(sizex<610&& isplaying==true){going.Size = new Size(sizex, 10);}}···
總體就這樣啦,不過,很多地方可能都沒說明,例如我是額外封裝了一個類 Lyric1的、還多寫了一個打開歌曲的通道,以及用tick判斷打開形式;上面涉及到的當前歌詞變大變色也沒詳細說明… … 代碼貼在下面,大家自己讀好了
代碼
···
//Lyric1.csusing System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;namespace kuwo { class Lyric1 {public int minute;public int second;public int tosecond;public string lyrics;}}//Form1.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.IO; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms;namespace kuwo { public partial class mainForm : Form {List<Lyric1> listLyric = new List<Lyric1>();List<Lyric1> listLyric1 = new List<Lyric1>();int tick = 0;public mainForm(){InitializeComponent();}Point downpoint;Point movepoint;bool ismoving;//實現(xiàn)鼠標控制窗口移動(1)private void Form1_MouseDown(object sender, MouseEventArgs e){downpoint = e.Location;ismoving = true;}private void timermove_Tick(object sender, EventArgs e){}//實現(xiàn)鼠標控制窗口移動(2)private void mainForm_MouseMove(object sender, MouseEventArgs e){if (ismoving == true){movepoint = e.Location;this.Location = new Point(this.Location.X + (movepoint.X - downpoint.X),this.Location.Y + (movepoint.Y - downpoint.Y));}}//鼠標按鍵抬起窗口停止移動private void mainForm_MouseUp(object sender, MouseEventArgs e){ismoving = false;}bool isplaying = true;string a;//歌曲總時間(字符串)//點擊播放:加載歌詞private void start_Click(object sender, EventArgs e){if (tick == 0){OpenFileDialog ofd = new OpenFileDialog();ofd.Filter = "mp3音樂|*.mp3|mp4音樂|*.mp4|flac文件|*.flac";//設置文件過濾,使之只能打開.mp3/.mp4/flac三種格式的音頻文件ofd.ShowDialog();string fileName = ofd.FileName;media.URL = fileName;media.Ctlcontrols.play();// start_Click(object sender, EventArgs e);timer2.Enabled = true;}tick = 1;timer1.Enabled = true;Encoding encode = Encoding.GetEncoding("GB2312");FileStream fs = new FileStream("陳一發(fā)兒-童話鎮(zhèn).lrc", FileMode.Open);StreamReader sr = new StreamReader(fs, encode);string s = sr.ReadLine();//int xPos = 100;int yPos = 50;string a= media.currentMedia.durationString;stime.Text = a;while (s != null){s = sr.ReadLine();if(s==null){break;}if (s.Equals("")){continue;}Lyric1 lyric =new Lyric1();lyric.minute =int.Parse(s.Substring(1,2));lyric.second = int.Parse(s.Substring(4,2));lyric.lyrics = s.Substring(10);lyric.tosecond = lyric.minute * 60 + lyric.second;//if(s.Substring(10)==null)//{// lyric.sminute = Convert.ToInt32(s.Substring(1, 1));//lyric.ssecond = Convert.ToInt32(s.Substring(5, 1));// }listLyric1.Add(lyric);yPos += 30;} if (isplaying){start.BackgroundImage = Properties.Resources.play_circle_o;isplaying = false;media.Ctlcontrols.pause();timer1.Enabled = false;//"C#openfiledialog 設置文件過濾"}else{start.BackgroundImage = Properties.Resources.pause_circle_o;isplaying = true;media.Ctlcontrols.play();timer1.Enabled = true;}fs.Close();sr.Close();}//打開本地資源private void startplay_Click(object sender, EventArgs e){tick = 2;OpenFileDialog ofd = new OpenFileDialog();ofd.Filter = "mp3音樂|*.mp3|mp4音樂|*.mp4|flac文件|*.flac";//設置文件過濾,使之只能打開.mp3/.mp4/flac三種格式的音頻文件ofd.ShowDialog();string fileName = ofd.FileName;media.URL = fileName;media.Ctlcontrols.play();// start_Click(object sender, EventArgs e);timer2.Enabled = true;timer1.Enabled = true;Encoding encode = Encoding.GetEncoding("GB2312");FileStream fs = new FileStream("陳一發(fā)兒-童話鎮(zhèn).lrc", FileMode.Open);StreamReader sr = new StreamReader(fs, encode);string s = sr.ReadLine();// int xPos = 100;int yPos = 50;string a = media.currentMedia.durationString;stime.Text = a;while (s != null){s = sr.ReadLine();if (s == null){break;}if (s.Equals("")){continue;}Lyric1 lyric = new Lyric1();lyric.minute = int.Parse(s.Substring(1, 2));lyric.second = int.Parse(s.Substring(4, 2));lyric.lyrics = s.Substring(10);lyric.tosecond = lyric.minute * 60 + lyric.second;//if(s.Substring(10)==null)//{// lyric.sminute = Convert.ToInt32(s.Substring(1, 1));//lyric.ssecond = Convert.ToInt32(s.Substring(5, 1));// }listLyric.Add(lyric);yPos += 30;}fs.Close();sr.Close();}//繪畫Graphics g;//繪圖對象int PointY=140;int current;//主窗口繪制歌詞private void mainForm_Paint(object sender, PaintEventArgs e){g = e.Graphics;if (tick == 2){for (int i = 0; i < listLyric.Count; i++){if (i == current)g.DrawString(listLyric[i].lyrics, new Font("微軟雅黑", 17), Brushes.LightBlue, 400, PointY + i * 50);elseg.DrawString(listLyric[i].lyrics, new Font("微軟雅黑", 14), Brushes.White, 400, PointY + i * 50);}}else{for (int i = 0; i < listLyric1.Count; i++){if (i == current)g.DrawString(listLyric1[i].lyrics, new Font("微軟雅黑", 17), Brushes.LightBlue, 400, PointY + i * 50);elseg.DrawString(listLyric1[i].lyrics, new Font("微軟雅黑", 14), Brushes.White, 400, PointY + i * 50);}}}double position;//時鐘:不停調(diào)用刷新主窗口private void timer1_Tick(object sender, EventArgs e){//獲取當前歌曲播放時間position = media.Ctlcontrols.currentPosition;int a = (int)position;int m = a / 60;int s = a % 60;time1.Text = m.ToString();time2.Text = s.ToString();PointY-=1;for (int i = 0; i < listLyric.Count-1; i++){double b = listLyric[i].tosecond;double c = listLyric[i+1].tosecond;// int positioni = (int)position;if (position > b && position < c)current = i;}this.Invalidate(UpdateRect);}Rectangle UpdateRect;//主窗口刷新private void mainForm_Load(object sender, EventArgs e){UpdateRect = new Rectangle(0, 0, this.ClientSize.Width,this.ClientSize.Height - panel1.Height);}int sizex = 42;private void timer2_Tick(object sender, EventArgs e){a = media.currentMedia.durationString;int timm = int.Parse(a.Substring(1,1));int tims = int.Parse(a.Substring(3,2));int tim = timm * 60 + tims;sizex += (610/ tim);if(sizex<610&& isplaying==true){going.Size = new Size(sizex, 10);}}//點擊播放private void lyricstart_Click(object sender, EventArgs e){// tick = 1;if (timer1.Enabled == true)timer1.Enabled = false;elsetimer1.Enabled = true;} } }···
總結
以上是生活随笔為你收集整理的C#/音乐播放器/带进度条/歌词滚动、颜色变化/桌面应用程序设计的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 做了一个app,返回三国武将的排序网页,
- 下一篇: 苏州园林年卡 办理与使用 附 苏州园林