基於C#的音樂播放器主Form完成代碼。本站提示廣大學習愛好者:(基於C#的音樂播放器主Form完成代碼)文章只能為提供參考,不一定能成為您想要的結果。以下是基於C#的音樂播放器主Form完成代碼正文
本文所述為一個由C#編寫的音樂播放器的主Form代碼,外面有一些小技能照樣不錯的,現同享給年夜家參考一下。外面有播放器配景設置、線程界說、挪用讀取文件目次辦法、播放時光計數器、設置LV配景、獲得播放歌曲、播放按鈕,切換播放or暫停、切換歌曲到下一首,挪用切歌辦法、顯示播放列表、歌詞局中、播放窗體最小化隱蔽到托盤設置、進度條轉動模塊、從歌曲列表中刪除文件等等功效。且各個功效模板均備有較為具體的正文,便於年夜家浏覽懂得。
法式重要代碼以下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; using System.Drawing.Imaging; using System.Threading; namespace MyMusicBox { public partial class MainForm : Form { public MainForm() { InitializeComponent(); SetStyle(ControlStyles.UserPaint, true); SetStyle(ControlStyles.OptimizedDoubleBuffer, true); SetStyle(ControlStyles.AllPaintingInWmPaint, true); SetStyle(ControlStyles.DoubleBuffer, true); } Song mySong = null;//播放的歌曲對象 ListViewItem itemLrc;//翻開歌詞途徑項 Bitmap bm ;//用於靜態設置LV配景 public SavingInfo sa = null;//耐久化信息類 Thread thread;//後台線程 #region 加載事宜 private void MainForm_Load(object sender, EventArgs e) { //挪用讀取設置裝備擺設文件辦法 Path.ReadPath(); //窗體配景設置為設置裝備擺設文件中的圖片 this.BackgroundImage = Image.FromFile(Path.bgPath); //挪用讀取文件目次辦法 Path.ReadSong(); //讀取播放列表 sa = new SavingInfo(); sa.LoadPlayList(); //綁定LV BindAllLV(); BindPlayLV(); SetLV(); SetLrcBG(); timer1.Enabled = Path.Playing; //顯示時光 lblTime.Text = System.DateTime.Now.ToString(); //兩個lbl用來依據歌曲總長和播放長度顯示進度,設置地位堆疊 lblRuning.Location = lblLong.Location; //啟動線程 thread = new Thread(Renovate); thread.IsBackground = true; thread.Start(); } #endregion #region 後台刷新 /// <summary> /// 時光計數狀況等刷新 /// </summary> private void Renovate() { //while (true) //{ // //計數 // Count(); // //獲得以後時光 // lblTime.Text = System.DateTime.Now.ToString(); // //無歌曲提醒,假如播放列表為空且以後播放列表為顯示狀況 // if (lvPlay.Items.Count == 0 && lvPlay.Visible) // lblTs.Visible = true; // else // lblTs.Visible = false; // Thread.Sleep(1000); //} } #endregion #region 設置LV配景 /// <summary> /// 設置LV配景 /// </summary> public void SetLV() { bm = new Bitmap(this.BackgroundImage, this.Width, this.Height); //繪制矩形,界說肇端地位和寬高 Rectangle r = new Rectangle(lvPlay.Location.X, lvPlay.Location.Y, lvPlay.Width, lvSong.Height); //按矩形尺寸和肇端地位截取bm的一部門 bm= bm.Clone(r,bm.PixelFormat); //把截取到的圖片設置為lv配景,到達與主窗體配景完善契合的後果 lvSong.BeginUpdate(); lvSong.BackgroundImage = (Image)bm; lvSong.EndUpdate(); lvPlay.BeginUpdate(); lvPlay.BackgroundImage = (Image)bm; lvPlay.EndUpdate(); } #endregion #region 獲得歌曲 /// <summary> /// 獲得播放歌曲 /// </summary> private void GetSong() { //吸收播放歌曲辦法,取得一個歌曲對象 this.mySong = PlayList.Play(); //假如歌曲對象不為空 if (mySong != null) { //播放器途徑設置為該歌曲途徑 wmp.URL = mySong.FileName; //挪用辦法,顯示歌曲名、歌手 lblName.Text = PlayList.GetName(); lblSinger.Text = PlayList.GetSinger(); mySong.SongState = SongPlayState.played;//修正播放狀況 //播放按鈕圖片修正為暫停圖片 btnPlay.Image = Image.FromFile("Images\\stop.png"); try { //讀取歌詞途徑 Path.ReadLrc(); AddLrc(); //啟動一個准時器,此准時器只掌握歌詞輪播 timer2.Start(); } catch (Exception) { LrcNull(); itemLrc = new ListViewItem(); itemLrc.SubItems.Add("找不到該歌曲歌詞文件!"); itemLrc.ForeColor = Color.Blue; itemLrc.Font = new Font("微軟雅黑", 14.25F, ((FontStyle)((FontStyle.Bold | FontStyle.Underline))), GraphicsUnit.Point, ((byte)(134))); lvLrc.Items.Add(itemLrc); } } } #endregion #region 主准時器 //准時器1 private void timer1_Tick(object sender, EventArgs e) { if (Path.songPath == "") { timer1.Enabled = false; return; } //假如以後無播放歌曲,挪用獲得歌曲的辦法 if (this.mySong == null) { GetSong(); } //主動下一首,假如以後播放終了 if (this.wmp.playState == WMPLib.WMPPlayState.wmppsStopped) { this.mySong = null; // 將歌曲設為空 Path.lrcPath = ""; Path.dicLrc.Clear(); PlayModel(); PlayList.PlayNext(); Thread.Sleep(1000); } // 切歌,以後有播放歌曲且播放狀況為cut if (this.mySong != null && this.mySong.SongState == SongPlayState.cut) { this.wmp.URL = "";//置空 timer2.Stop(); Path.dicLrc.Clear(); Path.lrcPath = ""; this.mySong = null; } //假如以後有播放歌曲,須要獲得的一些屬性 if (wmp.URL!="") { //設置以後播放歌曲色彩 SongColor(); //獲得音量控件顯示值,依據掌握刻度數目盤算 this.trackBar1.Value = wmp.settings.volume / 10; //歌曲時光顯示,一個是總長度,一個是已播放長度,字符串類型 lblAll.Text = wmp.currentMedia.durationString; lblRun.Text = wmp.Ctlcontrols.currentPositionString; //進度條,應用了兩個lbl控件,歌曲長度/已播放長度=lbl1的寬/lbl2的寬 //乘1000為避免數據太小湧現毛病 double temp = (wmp.currentMedia.duration*1000) / (wmp.Ctlcontrols.currentPosition*1000); double width = lblLong.Width; double avg = width / temp; //斷定>1為了避免avg數值小於int界線 if(avg>1) lblRuning.Width = Convert.ToInt32(avg); picRun.Left = lblRuning.Right; //托盤顯示播放歌曲 this.notifyIcon1.Text =lblName.Text + lblSinger.Text; } } #endregion #region 播放按鈕 /// <summary> /// 播放按鈕,切換播放or暫停 /// </summary> private void btnPlay_Click(object sender, EventArgs e) { if(!timer1.Enabled) timer1.Start(); //假如以後為正在播放 if (wmp.playState == WMPLib.WMPPlayState.wmppsPlaying) { //暫停播放 wmp.Ctlcontrols.pause(); //按鈕圖片修正為播放 btnPlay.Image = Image.FromFile("Images\\play.png"); //停滯掌握歌詞轉動的准時器 if (timer2.Enabled) timer2.Stop(); } else if (wmp.playState == WMPLib.WMPPlayState.wmppsPaused) { //開端播放 wmp.Ctlcontrols.play(); //按鈕圖片修正為暫停 btnPlay.Image = Image.FromFile("Images\\stop.png"); //啟動歌詞轉動 if (!timer2.Enabled&&Path.dicLrc.Count>0) timer2.Start(); } } #endregion #region 切歌 //切換下一首,挪用切歌辦法 private void btnRight_Click(object sender, EventArgs e) { PlayList.Cut(false); } //上一首,挪用切歌辦法 private void btnLeft_Click(object sender, EventArgs e) { PlayList.Cut(true); } #endregion #region 音量 //靜音,靜音!=音量為0 private void btnModel_Click(object sender, EventArgs e) { //假如播放器非靜音狀況,遠景文字顯示紅叉,狀況改成靜音 if (wmp.settings.mute == false) { btnSound.Text = "×"; wmp.settings.mute = true; } else//反之,已經是靜音狀況,清空紅叉,設置非靜音狀況 { btnSound.Text = ""; wmp.settings.mute = false; } } //設置音量,此事宜為掌握刻度值變更時 private void trackBar1_Scroll(object sender, EventArgs e) { //音量設置為掌握刻度*10(由於初始化的時刻是/10) wmp.settings.volume = this.trackBar1.Value * 10; } #endregion #region 歌曲列表操作 /// <summary> /// 綁定一切歌曲LV數據 /// </summary> private void BindAllLV() { //假如一切歌曲聚集為空,停止 if (PlayList.song == null) return; lvSong.Items.Clear(); //遍歷聚集綁定到一切歌曲LV foreach (Song s in PlayList.song) { ListViewItem item = new ListViewItem(s.Name); item.Tag = s; item.SubItems.Add(s.Singer); lvSong.Items.Add(item); } } //點擊顯示一切歌曲 private void tslblAll_Click(object sender, EventArgs e) { //隱蔽播放列表 lvPlay.Visible = false; //兩個LV地位堆疊 lvSong.Location = lvPlay.Location; //點擊後轉變配景色 tslblAll.BackColor = Color.Silver; tslblPlay.BackColor = Color.Transparent; //顯示一切歌曲LV lvSong.Visible = true; } //點擊顯示播放列表 private void tslblPlay_Click(object sender, EventArgs e) { //挪用綁定播放LV數據 BindPlayLV(); //配景色轉變 tslblPlay.BackColor = Color.Silver; tslblAll.BackColor = Color.Transparent; //隱蔽一切歌曲LV lvSong.Visible = false; //顯示播放LV lvPlay.Visible = true; } /// <summary> /// 綁定播放列表 /// </summary> private void BindPlayLV() { //假如播放聚集不為空 if (PlayList.play!=null) { lvPlay.Items.Clear(); //遍歷聚集綁定播放LV foreach (Song s in PlayList.play) { int id = lvPlay.Items.Count + 1; s.Id = id; ListViewItem item = new ListViewItem(id.ToString()); item.SubItems.Add(s.Name); item.Tag = s; lvPlay.Items.Add(item); } } } /// <summary> /// 歌曲計數 /// </summary> private void Count() { //if (lvSong.Visible) // lblCount.Text = "已選" + lvSong.CheckedItems.Count + "\\總" + lvSong.Items.Count; //if (lvPlay.Visible) // lblCount.Text = "總" + lvPlay.Items.Count; } #endregion #region 歌詞操作 /// <summary> /// 設置歌詞配景 /// </summary> public void SetLrcBG() { bm = new Bitmap(this.BackgroundImage, this.Width, this.Height); //繪制矩形,界說肇端地位和寬高 Rectangle r = new Rectangle(lvLrc.Location.X, lvLrc.Location.Y, lvLrc.Width, lvLrc.Height); //按矩形尺寸和肇端地位截取bm的一部門 bm = bm.Clone(r, PixelFormat.Undefined); //把截取到的圖片設置為lv配景,到達與主窗體配景完善契合的後果 lvLrc.BeginUpdate(); lvLrc.BackgroundImage = (Image)bm; lvLrc.EndUpdate(); } int nullCount;//記載空行地位 /// <summary> /// 設置空行,使歌詞局中 /// </summary> private void LrcNull() { lvLrc.Items.Clear(); nullCount = (int)Math.Ceiling(Convert.ToDouble(lvLrc.Height / 30 / 2)); for (int i = 0; i <= nullCount; i++) { lvLrc.Items.Add(""); } } /// <summary> /// 添加歌詞 /// </summary> private void AddLrc() { LrcNull(); foreach (string key in Path.dicLrc.Keys) { ListViewItem item = new ListViewItem(); item.SubItems.Add(Path.dicLrc[key]); item.Tag = key; lvLrc.Items.Add(item); } } //只掌握歌詞輪播 private void timer2_Tick(object sender, EventArgs e) { //挪用顯示歌詞的辦法 ShowLrc(); } /// <summary> /// 顯示歌詞 /// </summary> private void ShowLrc() { foreach (ListViewItem item in lvLrc.Items) { if (item.Tag == null) continue; if (item.Tag.ToString() == lblRun.Text ) { item.ForeColor = Color.DarkOrange; item.Font = new Font("漢文虎魄", 16F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(134))); lvLrc.Items[item.Index -1].ForeColor = Color.Silver; lvLrc.Items[item.Index - 1].Font = new Font("微軟雅黑", 12F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(134))); if (item.Index + 1<lvLrc.Items.Count) lvLrc.Items[item.Index+1].EnsureVisible(); } } } #endregion #region 窗體隱蔽到托盤設置 //最小化到托盤 private void btnMix_Click(object sender, EventArgs e) { this.Hide(); } //主窗體年夜小狀況變更時 private void MainForm_SizeChanged(object sender, EventArgs e) { if (this.WindowState == FormWindowState.Minimized) { //啟動一個控件,該掌握可讓法式在右下角顯示圖標,須要提早設置該掌握顯示的圖標圖片 this.notifyIcon1.Visible = true; } } //托盤顯示,雙擊顯示窗體 private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e) { ShowMain(); } /// <summary> /// 顯示主窗體 /// </summary> public void ShowMain() { if (!this.Visible) { this.TopMost = true; this.Show(); this.TopMost = false; } else { this.TopMost = true; this.TopMost = false; } } //托盤翻開窗體 private void tsmiShow_Click(object sender, EventArgs e) { ShowMain(); } //托盤加入 private void tsmiExit_Click(object sender, EventArgs e) { Application.Exit(); } #endregion #region 窗體位移 Point mouseOff;//鼠標挪動地位變量 bool leftFlag;//標志能否為左鍵 private void pnlMain_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { mouseOff = new Point(-e.X, -e.Y); //獲得變量的值 leftFlag = true; //點擊左鍵按下時標注為true; } } private void pnlMain_MouseMove(object sender, MouseEventArgs e) { if (leftFlag) { Point mouseSet = Control.MousePosition; mouseSet.Offset(mouseOff.X, mouseOff.Y); //設置挪動後的地位 Location = mouseSet; } } private void pnlMain_MouseUp(object sender, MouseEventArgs e) { if (leftFlag) { leftFlag = false;//釋放鼠標後標注為false; } } #endregion #region 進度條轉動塊挪動 int runX;//記載轉動塊初始地位 private void picRun_MouseDown(object sender, MouseEventArgs e) { runX = e.X; } //釋放鼠標位移,進度進步 private void picRun_MouseUp(object sender, MouseEventArgs e) { picRun.Left += e.X - runX - picRun.Width / 2; if (picRun.Left > lblLong.Right) picRun.Left = lblLong.Right; if(picRun.Left<lblLong.Left) picRun.Left = lblLong.Left; if (mySong != null) { lblRuning.Width = picRun.Right- lblRuning.Left; double temp = (lblLong.Width*1000.0 )/ (lblRuning.Width*1000.0); wmp.Ctlcontrols.currentPosition = wmp.currentMedia.duration / temp; } } #endregion #region 歌曲增刪操作 //添加歌曲到播放列表 private void tsmiAdd_Click(object sender, EventArgs e) { if (lvSong.CheckedItems.Count > 0) { foreach (ListViewItem item in lvSong.CheckedItems) { // 遍歷播放聚集,假如存在不添加 foreach (Song s in PlayList.play) { if (s.Name == item.Text) { PlayList.play.Remove(s); break; } } PlayList.play.Add(item.Tag as Song); } } } //一切歌曲:刪除選中歌曲 private void tsmiDel_Click(object sender, EventArgs e) { foreach (ListViewItem item in lvSong.Items) { if (item.Checked) { PlayList.song.Remove(item.Tag as Song); } } BindAllLV(); } //全選歌曲 private void tsmiCheck_Click(object sender, EventArgs e) { foreach (ListViewItem item in lvSong.Items) { if (tsmiCheck.Text == "全體選中") { item.Checked = true; if(item.Index+1==lvSong.Items.Count) tsmiCheck.Text = "撤消選中"; } else { item.Checked = false; if (item.Index + 1 == lvSong.Items.Count) tsmiCheck.Text = "全體選中"; } } } //選中項的同時選中復選框 private void lvSong_MouseDown(object sender, MouseEventArgs e) { if (lvSong.SelectedItems.Count > 0 && e.Button == MouseButtons.Left) { lvSong.SelectedItems[0].Checked = true; } } //播放列表刪除事宜 private void tsmiPlayDel_Click(object sender, EventArgs e) { foreach (ListViewItem item in lvPlay.Items) { if (item.Selected) { //PlayList.id = (lvPlay.Items[item.Index+1].Tag as Song).Id; //this.song = null; PlayList.play.Remove(item.Tag as Song); } } BindPlayLV(); } //清空播放列表 private void tsmiPlayClear_Click(object sender, EventArgs e) { PlayList.play.Clear(); this.mySong = null; BindPlayLV(); } //從播放列表刪除文件 private void tsmiPlayDelFile_Click(object sender, EventArgs e) { if (lvPlay.SelectedItems.Count > 0) { DelFile(lvPlay.SelectedItems[0]); } } /// <summary> /// 刪除某項對應文件 /// </summary> /// <param name="item"></param> private void DelFile(ListViewItem item) { try { string path = (item.Tag as Song).FileName; File.Delete(path); BindAllLV(); BindPlayLV(); } catch (Exception) { MessageBox.Show("該文件為只讀文件或用戶權限不敷,沒法刪除!"); } } //從一切歌曲中刪除文件 private void tsmiDelFile_Click(object sender, EventArgs e) { if (lvSong.SelectedItems.Count > 0) { DelFile(lvSong.SelectedItems[0]); } } //雙擊列表項播放歌曲 private void lvPlay_DoubleClick(object sender, EventArgs e) { if (!timer1.Enabled) { PlayList.id = (lvPlay.SelectedItems[0].Tag as Song).Id; timer1.Start(); } if (lvPlay.SelectedItems.Count > 0) { PlayList.id = (lvPlay.SelectedItems[0].Tag as Song).Id; this.mySong = null; } } #endregion #region 設置 //跳轉設置窗體 private void btnSet_Click(object sender, EventArgs e) { SetForm set = new SetForm(); set.parent = this;//把主窗體傳曩昔 set.Show(this); } #endregion #region 回滾 /// <summary> /// 途徑重置後 /// </summary> public void Back() { BindAllLV(); PlayList.Update(); timer1.Start(); } #endregion #region 加入 //封閉時保留列表 private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { if (Path.SaveList) sa.SavePlayList(); } //面板加入 private void btnExit_Click(object sender, EventArgs e) { if (Path.Exit) { Application.Exit(); } else { this.Hide(); } } #endregion #region 配景色彩等變換掌握 private void btnLeft_MouseEnter(object sender, EventArgs e) { ((Button)sender).BackgroundImage = Image.FromFile("Images//allbtn_highlight.png"); } private void btnLeft_MouseLeave(object sender, EventArgs e) { ((Button)sender).BackgroundImage = null; } private void tslblPlay_MouseEnter(object sender, EventArgs e) { ((ToolStripLabel)sender).BackgroundImage = Image.FromFile("Images//Album_bkg_wubian.png"); } private void tslblPlay_MouseLeave(object sender, EventArgs e) { ((ToolStripLabel)sender).BackgroundImage = null; } //選中轉變色彩 private void lvSong_ItemChecked(object sender, ItemCheckedEventArgs e) { foreach (ListViewItem item in lvSong.Items) { if (item.Checked) { item.ForeColor = Color.DarkOrange; } else { item.ForeColor = Color.DimGray; } } } /// <summary> /// 以後播放歌曲轉變色彩 /// </summary> private void SongColor() { foreach (ListViewItem item in lvPlay.Items) { if (item.SubItems[1].Text == lblName.Text) { item.ForeColor = Color.DeepSkyBlue; } else { item.ForeColor = Color.White; } } } #endregion #region 選擇歌詞 private void lvLrc_MouseDown(object sender, MouseEventArgs e) { if (lvLrc.SelectedItems.Count>0&&lvLrc.SelectedItems[0] == itemLrc) { if (Path.OpenLrc()) { AddLrc(); //啟動一個准時器,此准時器只掌握歌詞輪播 timer2.Start(); } } } #endregion #region 窗體最年夜化設置 Size now;//原始尺寸 Point pN;//原始地位 //最年夜化 private void btnMax_Click(object sender, EventArgs e) { Size max = new Size(Screen.PrimaryScreen.WorkingArea.Width - 10, Screen.PrimaryScreen.WorkingArea.Height - 20); if (this.Size != max) { pN = this.Location; now= this.Size; this.Size = max; this.Left = 5; this.Top = 5; btnMax.Image = Image.FromFile("Images//max1.png") ; } else { this.Location = pN; this.Size = now; btnMax.Image = Image.FromFile("Images//max.png"); } SetLV(); SetLrcBG(); } #endregion #region 歌詞、MV切換 private void tsmiLrc_Click(object sender, EventArgs e) { lvLrc.Visible = true; wmp.Visible = false; tsmiLrc.BackColor = Color.LawnGreen; tsmiMv.BackColor = Color.Transparent; } private void tsmiMv_Click(object sender, EventArgs e) { wmp.Location = lvLrc.Location; wmp.Size = lvLrc.Size; lvLrc.Visible = false; wmp.Visible = true; tsmiLrc.BackColor = Color.Transparent; tsmiMv.BackColor = Color.LawnGreen; } #endregion #region 播放形式切換 /// <summary> /// 播放形式 /// </summary> private void PlayModel() { if (tsmiOne.Checked) { PlayList.PlayOne(); } else if (tsmiSJ.Checked) { PlayList.PlayRandom(); } } private void tsmiXH_Click(object sender, EventArgs e) { ToolStripMenuItem ts=((ToolStripMenuItem)sender); foreach(ToolStripMenuItem item in tsmiModel.DropDownItems) { if (ts.Checked && ts == item) { continue; } else { item.Checked = false; } } } #endregion } }
願望本文所述示例對年夜家的C#法式開辟可以或許起到必定的自創價值。