本文利用C# 調用Windows自帶的Windows Media Player 打造一款屬於本身的音樂播放器,以供學習分享使用,若有不足之處,還請指正。html
Windows Media Player是微軟公司出品的一款免費的播放器,屬於Microsoft Windows的一個組件,一般簡稱"WMP",支持經過插件加強功能。版本Windows Media Player 12 隨 Windows 7及以上提供。能夠播放MP3,WMA,WAV等音頻文件。本文音樂播放器,主要是MP3文件。網絡
工具--》選擇工具箱項--》COM組件--》Windows Media Player,以下圖所示:異步
音樂播放器效果圖以下圖所示ide
加載皮膚工具
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 }
切換背景圖學習
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 }
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:23」 13 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:24」 25 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
關於AxWindowsMediaPlayer的屬性方法,也能夠參考豆丁文檔。ui
源碼下載連接this