C#基于winform實現(xiàn)音樂播放器
本文實例為大家分享了C#基于winform實現(xiàn)音樂播放器的具體代碼,供大家參考,具體內容如下
首先,右鍵工具箱的組件,找到選擇項,找到Windows Media Player組件并添加。
設計界面:
首先實現(xiàn)基本的功能
給“”老板播放器“的播放暫停添加代碼
MusicPlayer.Ctlcontrols.play(); ?//播放
MusicPlayer.Ctlcontrols.pause();//暫停
MusicPlayer.Ctlcontrols.stop();//停止
首先給Windows Media Player控件改名為MusicPlayer,并在程序加載時關閉自動播放和賦予一個默認的地址。
?private void Form1_Load(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? //在程序加載的時候,取消播放器的自動播放功能 ? ? ? ? ? ? MusicPlayer.settings.autoStart = false; ? ? ? ? ? ? MusicPlayer.URL = @"E:\CloudMusic\陳亮 - 無題.mp3"; ? ? ? ? ? ? label1.Image = Image.FromFile(@"C:\Users\14505\Desktop\繼續(xù).jpg"); ? ? ? ? }
接下來是播放鍵的按鈕
List<string> list = new List<string>();//用于儲存音樂的全路徑 ?private void btnPlayorPause_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? if (btnPlayorPause.Text == "播放") ? ? ? ? ? ? { ? ? ? ? ? ? ? ? if (b) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? //獲得選中的歌曲 ?讓音樂從頭播放 ? ? ? ? ? ? ? ? ? ? MusicPlayer.URL = list[listBox1.SelectedIndex]; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? MusicPlayer.Ctlcontrols.play(); ? ? ? ? ? ? ? ? btnPlayorPause.Text = "暫停"; ? ? ? ? ? ? } ? ? ? ? ? ? else if (btnPlayorPause.Text == "暫停") ? ? ? ? ? ? { ? ? ? ? ? ? ? ? MusicPlayer.Ctlcontrols.pause(); ? ? ? ? ? ? ? ? btnPlayorPause.Text = "播放"; ? ? ? ? ? ? ? ? b = false; ? ? ? ? ? ? } ? ? ? ? }
用list集合來存儲文件的路徑,并且listbox控件的items也對應這list,這樣我們可以通過點擊listbox選中內容(獲取它的索引)來找到對應索引的list集合中的路徑并播放。
給listbox添加雙擊事件:
?/// <summary> ? ? ? ? /// 雙擊播放對應的音樂 ? ? ? ? /// </summary> ? ? ? ? /// <param name="sender"></param> ? ? ? ? /// <param name="e"></param> ? ? ? ? private void listBox1_DoubleClick(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? if (listBox1.Items.Count == 0) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? MessageBox.Show("請首先原則音樂"); ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? } ? ? ? ? ? ? try ? ? ? ? ? ? { ? ? ? ? ? ? ? ? MusicPlayer.URL = list[listBox1.SelectedIndex]; ? ? ? ? ? ? ? ? MusicPlayer.Ctlcontrols.play(); ? ? ? ? ? ? ? ? btnPlayorPause.Text = "暫停"; ? ? ? ? ? ? ? ? lblinformation.Text = MusicPlayer.Ctlcontrols.currentPosition.ToString(); ? ? ? ? ? ? } ? ? ? ? ? ? catch { } ? ? ? ? }
接下來是打開按鈕,我們需要打開對話框選取想要的音樂文件
?/// <summary> ? ? ? ? /// 打開按鈕 ? ? ? ? /// </summary> ? ? ? ? /// <param name="sender"></param> ? ? ? ? /// <param name="e"></param> ? ? ? ? private void button4_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? OpenFileDialog ofd = new OpenFileDialog(); ? ? ? ? ? ? ofd.Title = "請選擇您的文件"; ? ? ? ? ? ? ofd.Filter = "音樂文件|*.mp3|全部文件|*.*"; ? ? ? ? ? ? ofd.InitialDirectory = @"E:\CloudMusic"; ? ? ? ? ? ? ofd.Multiselect = true; ? ? ? ? ? ? ofd.ShowDialog(); ? ? ? ? ? ? //獲得在文本框中選擇的全路徑 ? ? ? ? ? ? string[] path = ofd.FileNames; ? ? ? ? ? ? for (int i = 0; i < path.Length; i++) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? list.Add(path[i]); ? ? ? ? ? ? ? ? //將音樂文件的文件名存儲到listbox中 ? ? ? ? ? ? ? ? listBox1.Items.Add(Path.GetFileName(path[i])); ? ? ? ? ? ? } ? ? ? ? }
下面是上一首下一首的功能,我們只需要獲取listbox控件中當前選中項的索引,在使用lst即可
?/// <summary> ? ? ? ? /// 下一曲 ? ? ? ? /// </summary> ? ? ? ? /// <param name="sender"></param> ? ? ? ? /// <param name="e"></param> ? ? ? ? private void button5_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? //獲得當前選中的索引 ? ? ? ? ? ? int a = listBox1.SelectedIndex + 1; ? ? ? ? ? ? //清空所有選中的索引 ? 這里是因為我們開啟了多選屬性,才需要清理 ? ? ? ? ? ? listBox1.SelectedIndices.Clear(); ? ? ? ? ? ? if (a == listBox1.Items.Count) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? a = 0; ? ? ? ? ? ? } ? ? ? ? ? ? //將改變后的索引重新賦值給當前選中項的索引 ? ? ? ? ? ? listBox1.SelectedIndex = a; ? ? ? ? ? ? MusicPlayer.URL = list[a]; ? ? ? ? ? ? MusicPlayer.Ctlcontrols.play(); ? ? ? ? } ? ? ? ? /// <summary> ? ? ? ? /// 上一曲 ? ? ? ? /// </summary> ? ? ? ? /// <param name="sender"></param> ? ? ? ? /// <param name="e"></param> ? ? ? ? private void button6_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? int a = listBox1.SelectedIndex - 1; ? ? ? ? ? ? listBox1.SelectedIndices.Clear(); ? ? ? ? ? ? if (a < 0) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? a = listBox1.Items.Count - 1; ? ? ? ? ? ? } ? ? ? ? ? ? //將改變后的索引重新賦值給當前選中項的索引 ? ? ? ? ? ? listBox1.SelectedIndex = a; ? ? ? ? ? ? MusicPlayer.URL = list[a]; ? ? ? ? ? ? MusicPlayer.Ctlcontrols.play(); ? ? ? ? }
給listbox控件添加一個右鍵菜單,我們需要多選刪除功能。
這里必須先清除集合中的內容,再清除listbox控件中的內容,否則會引起程序的異常。
?/// <summary> ? ? ? ? /// 點擊刪除選中項 ? ? ? ? /// </summary> ? ? ? ? /// <param name="sender"></param> ? ? ? ? /// <param name="e"></param> ? ? ? ? private void 刪除ToolStripMenuItem_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? //要刪除列表中的選中項 ? ? ? ? ? ? //先刪集合 ? ? ? ? ? ? //首先獲得要刪除的歌曲的數(shù)量 ? ? ? ? ? ? int count = listBox1.SelectedItems.Count; ? ? ? ? ? ? for (int i = 0; i < count; i++) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? //先刪集合 ? ? ? ? ? ? ? ? list.RemoveAt(listBox1.SelectedIndex); ? ? ? ? ? ? ? ? //在刪列表 ? ? ? ? ? ? ? ? listBox1.Items.RemoveAt(listBox1.SelectedIndex); ? ? ? ? ? ? } ? ? ? ? }
接下來是靜音和外放按鈕,這里我使用label控件添加了圖片(百度自行找播放和暫停的圖片即可)
?/// <summary> ? ? ? ? /// 點擊放音或靜音 ? ? ? ? /// </summary> ? ? ? ? /// <param name="sender"></param> ? ? ? ? /// <param name="e"></param> ? ? ? ? private void label1_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? if (label1.Tag.ToString() == "1") ? ? ? ? ? ? { ? ? ? ? ? ? ? ? //目的:讓你靜音 ? ? ? ? ? ? ? ? MusicPlayer.settings.mute = true;//靜音 ? ? ? ? ? ? ? ? //顯示靜音的圖片 ? ? ? ? ? ? ? ? label1.Image = Image.FromFile(@"C:\Users\14505\Desktop\暫停.jpg"); ? ? ? ? ? ? ? ? label1.Tag = "2"; ? ? ? ? ? ? } ? ? ? ? ? ? else ? ? ? ? ? ? { ? ? ? ? ? ? ? ? MusicPlayer.settings.mute = false; ? ? ? ? ? ? ? ? //顯示放音圖片 ? ? ? ? ? ? ? ? label1.Image = Image.FromFile(@"C:\Users\14505\Desktop\繼續(xù).jpg"); ? ? ? ? ? ? ? ? label1.Tag = 1; ? ? ? ? ? ? } ? ? ? ? }
接下來要加一個播放完自動下一首的功能
我這里使用了歌曲全部時常和當前播放時長去比較,當前播放時常+1等于全部時長時,我們就切換下一首
或者使用bool判斷控件的播放狀態(tài)也是一樣的道理
private void timer1_Tick(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? ? ?//如果播放器的狀態(tài)時正在播放中 ? ? ? ? ? ? if (MusicPlayer.playState == WMPLib.WMPPlayState.wmppsPlaying) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? lblinformation.Text = MusicPlayer.currentMedia.duration.ToString() + "\r\n" + MusicPlayer.currentMedia.durationString + "\r\n" + MusicPlayer.Ctlcontrols.currentPositionString; ? ? ? ? ? ? ? ? double b1 = double.Parse(MusicPlayer.currentMedia.duration.ToString()); ? ? ? ? ? ? ? ? double b2 = double.Parse(MusicPlayer.Ctlcontrols.currentPosition.ToString())+1; ? ? ? ? ? ? ? ? //如果歌曲當前的播放時間等于歌曲的總時間,自動播放下一曲 ? ?//比較時間的值 ? ? ? ? ? ? ? ? if (b1<=b2) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? //獲得當前選中的索引 ? ? ? ? ? ? ? ? ? ? int a = listBox1.SelectedIndex + 1; ? ? ? ? ? ? ? ? ? ? //清空所有選中的索引 ? ? ? ? ? ? ? ? ? ? listBox1.SelectedIndices.Clear(); ? ? ? ? ? ? ? ? ? ? if (a == listBox1.Items.Count) ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? a = 0; ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? //將改變后的索引重新賦值給當前選中項的索引 ? ? ? ? ? ? ? ? ? ? listBox1.SelectedIndex = a; ? ? ? ? ? ? ? ? ? ? MusicPlayer.URL = list[a]; ? ? ? ? ? ? ? ? ? ? MusicPlayer.Ctlcontrols.play(); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? //比較時間的值 ? ? ? ? ?? ? ? ? ? }
運行截圖:
本想添加一個顯示歌曲歌詞的功能的,但是找了半天也沒找到歌詞文件的下載方式。
這樣一個簡單的可以自用的播放器就做好啦!
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
C#使用RestSharp實現(xiàn)封裝常用的http請求方法
這篇文章主要為大家詳細介紹了C#如何使用RestSharp實現(xiàn)封裝常用的http請求方法,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下2024-02-02C# 開發(fā)(創(chuàng)藍253)手機短信驗證碼接口的實例
下面小編就為大家分享一篇C# 開發(fā)(創(chuàng)藍253)手機短信驗證碼接口的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01