C# 一款屬於自己的音樂播放器


本文利用C# 調用Windows自帶的Windows Media Player 打造一款屬於自己的音樂播放器,以供學習分享使用,如有不足之處,還請指正。

概述

Windows Media Player是微軟公司出品的一款免費的播放器,屬於Microsoft Windows的一個組件,通常簡稱"WMP",支持通過插件增強功能。版本Windows Media Player 12 隨 Windows 7及以上提供。可以播放MP3,WMA,WAV等音頻文件。本文音樂播放器,主要是MP3文件。

涉及知識點

  1. 加載COM組件Windows Media Player控件到Winform窗體中。
  2. AxWindowsMediaPlayer控件的常見用法
  3. BackgroundWorker 在單獨的線程上執行操作。異步操作界面上的內容。
  4. IrisSkin 皮膚控件,主要利用SkinEngine來美化界面,達到風格統一的效果。

設計思路

  1. 通過AxWindowsMediaPlayer實現音樂的播放。
  2. 通過PictureBox實現歌曲背景圖的顯示。
  3. 通過BackgroundWorker 實現背景圖的切換更新,以及歌詞的實時顯示。
  4. 通過Label顯示歌詞,以及播放時間。
  5. 通過DataGridView實現歌曲列表。
  6. 通過IrisSkin來美化界面。

添加Windows Media Player組件

工具--》選擇工具箱項--》COM組件--》Windows Media Player,如下圖所示:

音樂播放器效果圖

音樂播放器效果圖如下圖所示

核心代碼

加載皮膚

 1   /// <summary>
 2         /// 初始化皮膚
 3         /// </summary>
 4         private void InitInfo()
 5         {
 6             this.skinEngine = new SkinEngine();
 7             this.skinEngine.SkinFile = AppDomain.CurrentDomain.BaseDirectory + @"Skins\mp10.ssk";
 8             this.skinEngine.DisableTag = 9999;
 9             ReadSkinFile();
10         }
View Code

切換背景圖

 1  /// <summary>
 2         /// 背景圖切換
 3         /// </summary>
 4         /// <param name="sender"></param>
 5         /// <param name="e"></param>
 6         private void BgWorker1_DoWork(object sender, DoWorkEventArgs e)
 7         {
 8             while (isRunning) {
 9                 if (bgUrls != null && bgUrls.Length > 0) {
10                     this.curIndex = (this.curIndex % bgUrls.Length);
11                     this.pbImage.Image = Image.FromStream(HttpWebRequest.Create(bgUrls[this.curIndex]).GetResponse().GetResponseStream());
12                     this.pbImage.SizeMode = PictureBoxSizeMode.StretchImage;
13                     this.curIndex++;
14                     //
15                     this.lblTime.Invoke(new Action(() =>
16                     {
17                         this.lblTime.Text = string.Format("{0}/{1}", this.axWindowsMediaPlayer1.Ctlcontrols.currentPositionString, this.axWindowsMediaPlayer1.currentMedia.durationString);
18                         //this.lblTime.ForeColor = Color.White;
19                     }));
20                     
21                 }
22                 Thread.Sleep(2000);
23             }
24         }
View Code

AxWindowsMediaPlayer相關屬性方法

 1 [基本屬性]   
 2 URL:String; 指定媒體位置,本機或網絡地址 
 3 uiMode:String; 播放器界面模式,可為Full, Mini, None, Invisible 
 4 playState:integer; 播放狀態,1=停止,2=暫停,3=播放,6=正在緩沖,9=正在連接,10=准備就緒 
 5 enableContextMenu:Boolean; 啟用/禁用右鍵菜單 
 6 fullScreen:boolean; 是否全屏顯示 
 7 //播放器基本控制 
 8 Ctlcontrols.play; 播放 
 9 Ctlcontrols.pause; 暫停 
10 Ctlcontrols.stop; 停止 
11 Ctlcontrols.currentPosition:double; 當前進度 
12 Ctlcontrols.currentPositionString:string; 當前進度,字符串格式。如“00:2313 Ctlcontrols.fastForward; 快進 
14 Ctlcontrols.fastReverse; 快退 
15 Ctlcontrols.next; 下一曲 
16 Ctlcontrols.previous; 上一曲 
17 [settings] wmp.settings //播放器基本設置 
18 settings.volume:integer; 音量,0-100 
19 settings.autoStart:Boolean; 是否自動播放 
20 settings.mute:Boolean; 是否靜音 
21 settings.playCount:integer; 播放次數 
22 [currentMedia] wmp.currentMedia //當前媒體屬性 
23 currentMedia.duration:double; 媒體總長度 
24 currentMedia.durationString:string; 媒體總長度,字符串格式。如“03:2425 currentMedia.getItemInfo(const string); 獲取當前媒體信息"Title"=媒體標題,"Author"=藝術家,"Copyright"=版權信息,"Description"=媒體內容描述, "Duration"=持續時間(秒),"FileSize"=文件大小,"FileType"=文件類型,"sourceURL"=原始地址 
26 currentMedia.setItemInfo(const string); 通過屬性名設置媒體信息 
27 currentMedia.name:string; 同 currentMedia.getItemInfo("Title") 
28 [currentPlaylist] wmp.currentPlaylist //當前播放列表屬性 
29 currentPlaylist.count:integer; 當前播放列表所包含媒體數 
30 currentPlaylist.Item[integer]; 獲取或設置指定項目媒體信息,其子屬性同wmp.currentMedia 
31 axWindowsMediaPlayer1.currentMedia.sourceURL; //獲取正在播放的媒體文件的路徑
32 axWindowsMediaPlayer1.currentMedia.name;          //獲取正在播放的媒體文件的名稱
33 axWindowsMediaPlayer1.Ctlcontrols.Play          播放 
34 axWindowsMediaPlayer1.Ctlcontrols.Stop          停止 
35 axWindowsMediaPlayer1.Ctlcontrols.Pause          暫停 
36 axWindowsMediaPlayer1.Ctlcontrols.PlayCount        文件播放次數 
37 axWindowsMediaPlayer1.Ctlcontrols.AutoRewind       是否循環播放 
38 axWindowsMediaPlayer1.Ctlcontrols.Balance         聲道 
39 axWindowsMediaPlayer1.Ctlcontrols.Volume         音量 
40 axWindowsMediaPlayer1.Ctlcontrols.Mute          靜音 
41 axWindowsMediaPlayer1.Ctlcontrols.EnableContextMenu    是否允許在控件上點擊鼠標右鍵時彈出快捷菜單 
42 axWindowsMediaPlayer1.Ctlcontrols.AnimationAtStart    是否在播放前先播放動畫 
43 axWindowsMediaPlayer1.Ctlcontrols.ShowControls      是否顯示控件工具欄 
44 axWindowsMediaPlayer1.Ctlcontrols.ShowAudioControls    是否顯示聲音控制按鈕 
45 axWindowsMediaPlayer1.Ctlcontrols.ShowDisplay       是否顯示數據文件的相關信息 
46 axWindowsMediaPlayer1.Ctlcontrols.ShowGotoBar       是否顯示Goto欄 
47 axWindowsMediaPlayer1.Ctlcontrols.ShowPositionControls  是否顯示位置調節按鈕 
48 axWindowsMediaPlayer1.Ctlcontrols.ShowStatusBar      是否顯示狀態欄 
49 axWindowsMediaPlayer1.Ctlcontrols.ShowTracker       是否顯示進度條 
50 axWindowsMediaPlayer1.Ctlcontrols.FastForward       快進 
51 axWindowsMediaPlayer1.Ctlcontrols.FastReverse       快退 
52 axWindowsMediaPlayer1.Ctlcontrols.Rate          快進/快退速率 
53 axWindowsMediaPlayer1.AllowChangeDisplaySize 是否允許自由設置播放圖象大小 
54 axWindowsMediaPlayer1.DisplaySize       設置播放圖象大小 
55     1-MpDefaultSize         原始大小 
56     2-MpHalfSize           原始大小的一半 
57     3-MpDoubleSize          原始大小的兩倍 
58     4-MpFullScreen          全屏 
59     5-MpOneSixteenthScreen      屏幕大小的1/16 
60     6-MpOneFourthScreen       屏幕大小的1/4 
61     7-MpOneHalfScreen        屏幕大小的1/2 
62 axWindowsMediaPlayer1.ClickToPlay       是否允許單擊播放窗口啟動Media Player
View Code

備注

關於AxWindowsMediaPlayer的屬性方法,也可以參考豆丁文檔。

源碼下載鏈接

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM