欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2022年02月10日 10:28:29   作者:向上的青春233  
這篇文章主要為大家詳細介紹了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# base關鍵字的具體使用

    c# base關鍵字的具體使用

    base關鍵字用于從派生類中訪問基類的成員,本文主要介紹了c# base關鍵字的具體使用,文中通過示例代碼介紹的非常詳細,需要的朋友們下面隨著小編來一起學習學習吧
    2024-03-03
  • C# 表達式樹Expression Trees的知識梳理

    C# 表達式樹Expression Trees的知識梳理

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

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

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

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

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

    VS2012 程序打包部署圖文詳解

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

    C#托管內存與非托管內存之間的轉換的實例講解

    在本篇文章里小編給大家整理了關于C#托管內存與非托管內存之間的轉換的實例以及相關知識點,需要的朋友們學習下。
    2019-08-08
  • c#文檔圖片自動糾偏

    c#文檔圖片自動糾偏

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

    C# 開發(fā)(創(chuàng)藍253)手機短信驗證碼接口的實例

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

    深入理解C# abstract和virtual關鍵字

    深入理解C# abstract和virtual關鍵字,學習c#的朋友可以參考下。
    2011-06-06
  • C#中方法的詳細介紹

    C#中方法的詳細介紹

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

最新評論