亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C#基于winform實(shí)現(xiàn)音樂(lè)播放器

 更新時(shí)間:2022年02月10日 10:28:29   作者:向上的青春233  
這篇文章主要為大家詳細(xì)介紹了C#基于winform實(shí)現(xiàn)音樂(lè)播放器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(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)是打開按鈕,我們需要打開對(duì)話框選取想要的音樂(lè)文件

?/// <summary>
? ? ? ? /// 打開按鈕
? ? ? ? /// </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)槲覀冮_啟了多選屬性,才需要清理
? ? ? ? ? ? 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# base關(guān)鍵字的具體使用

    c# base關(guān)鍵字的具體使用

    base關(guān)鍵字用于從派生類中訪問(wèn)基類的成員,本文主要介紹了c# base關(guān)鍵字的具體使用,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2024-03-03
  • C# 表達(dá)式樹Expression Trees的知識(shí)梳理

    C# 表達(dá)式樹Expression Trees的知識(shí)梳理

    本篇文章主要介紹了表達(dá)式樹 Expression Trees的基礎(chǔ)知識(shí):Lambda 表達(dá)式創(chuàng)建表達(dá)式樹;API 創(chuàng)建表達(dá)式樹;編譯表達(dá)式樹;執(zhí)行表達(dá)式樹;修改表達(dá)式樹等等,具有一定的參考價(jià)值,下面跟著小編一起來(lái)看下吧
    2017-01-01
  • C#讀取文件MD5值的實(shí)現(xiàn)代碼

    C#讀取文件MD5值的實(shí)現(xiàn)代碼

    這篇文章主要介紹了C#讀取文件MD5值的實(shí)現(xiàn)代碼,有了這個(gè)核心代碼,就可以實(shí)現(xiàn)校驗(yàn)文件MD5值的一些程序了,需要的朋友可以參考下
    2014-08-08
  • C#使用RestSharp實(shí)現(xiàn)封裝常用的http請(qǐng)求方法

    C#使用RestSharp實(shí)現(xiàn)封裝常用的http請(qǐng)求方法

    這篇文章主要為大家詳細(xì)介紹了C#如何使用RestSharp實(shí)現(xiàn)封裝常用的http請(qǐng)求方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2024-02-02
  • VS2012 程序打包部署圖文詳解

    VS2012 程序打包部署圖文詳解

    VS2012雖然沒(méi)有集成打包工具,但它為我們提供了下載的端口,需要我們手動(dòng)安裝一個(gè)插件InstallShield。網(wǎng)上有很多第三方的打包工具,但為什么偏要使用微軟提供的呢
    2016-12-12
  • C#托管內(nèi)存與非托管內(nèi)存之間的轉(zhuǎn)換的實(shí)例講解

    C#托管內(nèi)存與非托管內(nèi)存之間的轉(zhuǎn)換的實(shí)例講解

    在本篇文章里小編給大家整理了關(guān)于C#托管內(nèi)存與非托管內(nèi)存之間的轉(zhuǎn)換的實(shí)例以及相關(guān)知識(shí)點(diǎn),需要的朋友們學(xué)習(xí)下。
    2019-08-08
  • c#文檔圖片自動(dòng)糾偏

    c#文檔圖片自動(dòng)糾偏

    最近找到一個(gè)不錯(cuò)的文檔圖片自動(dòng)糾偏的方法,現(xiàn)在跟大家分享一下,需要的朋友可以參考下
    2014-03-03
  • C# 開發(fā)(創(chuàng)藍(lán)253)手機(jī)短信驗(yàn)證碼接口的實(shí)例

    C# 開發(fā)(創(chuàng)藍(lán)253)手機(jī)短信驗(yàn)證碼接口的實(shí)例

    下面小編就為大家分享一篇C# 開發(fā)(創(chuàng)藍(lán)253)手機(jī)短信驗(yàn)證碼接口的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-01-01
  • 深入理解C# abstract和virtual關(guān)鍵字

    深入理解C# abstract和virtual關(guān)鍵字

    深入理解C# abstract和virtual關(guān)鍵字,學(xué)習(xí)c#的朋友可以參考下。
    2011-06-06
  • C#中方法的詳細(xì)介紹

    C#中方法的詳細(xì)介紹

    本篇文章介紹了,C#中方法的詳細(xì)說(shuō)明。需要的朋友參考下
    2013-04-04

最新評(píng)論