C#基于winform實(shí)現(xiàn)音樂(lè)播放器
本文實(shí)例為大家分享了C#基于winform實(shí)現(xiàn)音樂(lè)播放器的具體代碼,供大家參考,具體內(nèi)容如下
首先,右鍵工具箱的組件,找到選擇項(xiàng),找到Windows Media Player組件并添加。
設(shè)計(jì)界面:

首先實(shí)現(xiàn)基本的功能
給“”老板播放器“的播放暫停添加代碼
MusicPlayer.Ctlcontrols.play(); ?//播放
MusicPlayer.Ctlcontrols.pause();//暫停
MusicPlayer.Ctlcontrols.stop();//停止
首先給Windows Media Player控件改名為MusicPlayer,并在程序加載時(shí)關(guān)閉自動(dòng)播放和賦予一個(gè)默認(rèn)的地址。
?private void Form1_Load(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? //在程序加載的時(shí)候,取消播放器的自動(dòng)播放功能
? ? ? ? ? ? MusicPlayer.settings.autoStart = false;
? ? ? ? ? ? MusicPlayer.URL = @"E:\CloudMusic\陳亮 - 無(wú)題.mp3";
? ? ? ? ? ? label1.Image = Image.FromFile(@"C:\Users\14505\Desktop\繼續(xù).jpg");
? ? ? ? }接下來(lái)是播放鍵的按鈕
List<string> list = new List<string>();//用于儲(chǔ)存音樂(lè)的全路徑
?private void btnPlayorPause_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? if (btnPlayorPause.Text == "播放")
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if (b)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? //獲得選中的歌曲 ?讓音樂(lè)從頭播放
? ? ? ? ? ? ? ? ? ? MusicPlayer.URL = list[listBox1.SelectedIndex];
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? MusicPlayer.Ctlcontrols.play();
? ? ? ? ? ? ? ? btnPlayorPause.Text = "暫停";
? ? ? ? ? ? }
? ? ? ? ? ? else if (btnPlayorPause.Text == "暫停")
? ? ? ? ? ? {
? ? ? ? ? ? ? ? MusicPlayer.Ctlcontrols.pause();
? ? ? ? ? ? ? ? btnPlayorPause.Text = "播放";
? ? ? ? ? ? ? ? b = false;
? ? ? ? ? ? }
? ? ? ? }用list集合來(lái)存儲(chǔ)文件的路徑,并且listbox控件的items也對(duì)應(yīng)這list,這樣我們可以通過(guò)點(diǎn)擊listbox選中內(nèi)容(獲取它的索引)來(lái)找到對(duì)應(yīng)索引的list集合中的路徑并播放。
給listbox添加雙擊事件:
?/// <summary>
? ? ? ? /// 雙擊播放對(duì)應(yīng)的音樂(lè)
? ? ? ? /// </summary>
? ? ? ? /// <param name="sender"></param>
? ? ? ? /// <param name="e"></param>
? ? ? ? private void listBox1_DoubleClick(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? if (listBox1.Items.Count == 0)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? MessageBox.Show("請(qǐng)首先原則音樂(lè)");
? ? ? ? ? ? ? ? return;
? ? ? ? ? ? }
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? MusicPlayer.URL = list[listBox1.SelectedIndex];
? ? ? ? ? ? ? ? MusicPlayer.Ctlcontrols.play();
? ? ? ? ? ? ? ? btnPlayorPause.Text = "暫停";
? ? ? ? ? ? ? ? lblinformation.Text = MusicPlayer.Ctlcontrols.currentPosition.ToString();
? ? ? ? ? ? }
? ? ? ? ? ? catch { }
? ? ? ? }接下來(lái)是打開(kāi)按鈕,我們需要打開(kāi)對(duì)話框選取想要的音樂(lè)文件
?/// <summary>
? ? ? ? /// 打開(kāi)按鈕
? ? ? ? /// </summary>
? ? ? ? /// <param name="sender"></param>
? ? ? ? /// <param name="e"></param>
? ? ? ? private void button4_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? OpenFileDialog ofd = new OpenFileDialog();
? ? ? ? ? ? ofd.Title = "請(qǐng)選擇您的文件";
? ? ? ? ? ? ofd.Filter = "音樂(lè)文件|*.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]);
? ? ? ? ? ? ? ? //將音樂(lè)文件的文件名存儲(chǔ)到listbox中
? ? ? ? ? ? ? ? listBox1.Items.Add(Path.GetFileName(path[i]));
? ? ? ? ? ? }
? ? ? ? }下面是上一首下一首的功能,我們只需要獲取listbox控件中當(dāng)前選中項(xiàng)的索引,在使用lst即可
?/// <summary>
? ? ? ? /// 下一曲
? ? ? ? /// </summary>
? ? ? ? /// <param name="sender"></param>
? ? ? ? /// <param name="e"></param>
? ? ? ? private void button5_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? //獲得當(dāng)前選中的索引
? ? ? ? ? ? int a = listBox1.SelectedIndex + 1;
? ? ? ? ? ? //清空所有選中的索引 ? 這里是因?yàn)槲覀冮_(kāi)啟了多選屬性,才需要清理
? ? ? ? ? ? listBox1.SelectedIndices.Clear();
? ? ? ? ? ? if (a == listBox1.Items.Count)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? a = 0;
? ? ? ? ? ? }
? ? ? ? ? ? //將改變后的索引重新賦值給當(dāng)前選中項(xiàng)的索引
? ? ? ? ? ? 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;
? ? ? ? ? ? }
? ? ? ? ? ? //將改變后的索引重新賦值給當(dāng)前選中項(xiàng)的索引
? ? ? ? ? ? listBox1.SelectedIndex = a;
? ? ? ? ? ? MusicPlayer.URL = list[a];
? ? ? ? ? ? MusicPlayer.Ctlcontrols.play();
? ? ? ? }給listbox控件添加一個(gè)右鍵菜單,我們需要多選刪除功能。
這里必須先清除集合中的內(nèi)容,再清除listbox控件中的內(nèi)容,否則會(huì)引起程序的異常。
?/// <summary>
? ? ? ? /// 點(diǎn)擊刪除選中項(xiàng)
? ? ? ? /// </summary>
? ? ? ? /// <param name="sender"></param>
? ? ? ? /// <param name="e"></param>
? ? ? ? private void 刪除ToolStripMenuItem_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? //要?jiǎng)h除列表中的選中項(xiàng)
? ? ? ? ? ? //先刪集合
? ? ? ? ? ? //首先獲得要?jiǎng)h除的歌曲的數(shù)量
? ? ? ? ? ? int count = listBox1.SelectedItems.Count;
? ? ? ? ? ? for (int i = 0; i < count; i++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? //先刪集合
? ? ? ? ? ? ? ? list.RemoveAt(listBox1.SelectedIndex);
? ? ? ? ? ? ? ? //在刪列表
? ? ? ? ? ? ? ? listBox1.Items.RemoveAt(listBox1.SelectedIndex);
? ? ? ? ? ? }
? ? ? ? }接下來(lái)是靜音和外放按鈕,這里我使用label控件添加了圖片(百度自行找播放和暫停的圖片即可)
?/// <summary>
? ? ? ? /// 點(diǎn)擊放音或靜音
? ? ? ? /// </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;
? ? ? ? ? ? }
? ? ? ? }接下來(lái)要加一個(gè)播放完自動(dòng)下一首的功能
我這里使用了歌曲全部時(shí)常和當(dāng)前播放時(shí)長(zhǎng)去比較,當(dāng)前播放時(shí)常+1等于全部時(shí)長(zhǎng)時(shí),我們就切換下一首
或者使用bool判斷控件的播放狀態(tài)也是一樣的道理
private void timer1_Tick(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? ? ?//如果播放器的狀態(tài)時(shí)正在播放中
? ? ? ? ? ? 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;
? ? ? ? ? ? ? ? //如果歌曲當(dāng)前的播放時(shí)間等于歌曲的總時(shí)間,自動(dòng)播放下一曲 ? ?//比較時(shí)間的值
? ? ? ? ? ? ? ? if (b1<=b2)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? //獲得當(dāng)前選中的索引
? ? ? ? ? ? ? ? ? ? int a = listBox1.SelectedIndex + 1;
? ? ? ? ? ? ? ? ? ? //清空所有選中的索引
? ? ? ? ? ? ? ? ? ? listBox1.SelectedIndices.Clear();
? ? ? ? ? ? ? ? ? ? if (a == listBox1.Items.Count)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? a = 0;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? //將改變后的索引重新賦值給當(dāng)前選中項(xiàng)的索引
? ? ? ? ? ? ? ? ? ? listBox1.SelectedIndex = a;
? ? ? ? ? ? ? ? ? ? MusicPlayer.URL = list[a];
? ? ? ? ? ? ? ? ? ? MusicPlayer.Ctlcontrols.play();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? //比較時(shí)間的值
? ? ? ? ??
? ? ? ? }運(yùn)行截圖:

本想添加一個(gè)顯示歌曲歌詞的功能的,但是找了半天也沒(méi)找到歌詞文件的下載方式。
這樣一個(gè)簡(jiǎn)單的可以自用的播放器就做好啦!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#從foreach語(yǔ)句中枚舉元素看數(shù)組詳解
這篇文章主要給大家介紹了關(guān)于C#從foreach語(yǔ)句中枚舉元素看數(shù)組的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-05-05
C#實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器小程序
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器小程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
c#通過(guò)unicode編碼判斷字符是否為中文示例分享
本文介紹了c#通過(guò)unicode編碼判斷字符是否為中文的示例,在unicode字符串中,中文的范圍是在4E00..9FFF:CJK Unified Ideographs。通過(guò)對(duì)字符的unicode編碼進(jìn)行判斷來(lái)確定字符是否為中文2014-01-01
IIS下調(diào)用證書(shū)出現(xiàn)異常的解決方法 (C#)
這篇文章主要為大家詳細(xì)介紹了IIS下調(diào)用證書(shū)出現(xiàn)異常的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
C#實(shí)現(xiàn)23種常見(jiàn)的設(shè)計(jì)模式的示例詳解
設(shè)計(jì)模式通常分為三個(gè)主要類(lèi)別:創(chuàng)建型模式、結(jié)構(gòu)型模式和行為型模式,這些模式是用于解決常見(jiàn)的對(duì)象導(dǎo)向設(shè)計(jì)問(wèn)題的最佳實(shí)踐,本文為大家整理了23種常見(jiàn)的設(shè)計(jì)模式的實(shí)現(xiàn)代碼,需要的可以參考一下2023-06-06
C#實(shí)現(xiàn)文件壓縮與解壓的方法示例【ZIP格式】
這篇文章主要介紹了C#實(shí)現(xiàn)文件壓縮與解壓的方法,結(jié)合具體實(shí)例形式分析了C#針對(duì)文件進(jìn)行zip格式壓縮與解壓縮的相關(guān)操作技巧,需要的朋友可以參考下2017-06-06
解析StreamReader與文件亂碼問(wèn)題的解決方法
本篇文章是對(duì)StreamReader與文件亂碼問(wèn)題的解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
C#使用正則表達(dá)式過(guò)濾html標(biāo)簽
最近在開(kāi)發(fā)一個(gè)項(xiàng)目,其中有需求要求我們把一段html轉(zhuǎn)換為一般文本返回,使用正則表達(dá)式是明智的選擇,下面小編給介紹下C#使用正則表達(dá)式過(guò)濾html標(biāo)簽,需要的朋友參考下2016-08-08

