C# 一款属于自己的音乐播放器

时间:2023-03-08 21:28:49

本文利用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,如下图所示:

C# 一款属于自己的音乐播放器

音乐播放器效果图

音乐播放器效果图如下图所示

C# 一款属于自己的音乐播放器

核心代码

加载皮肤

   /// <summary>
/// 初始化皮肤
/// </summary>
private void InitInfo()
{
this.skinEngine = new SkinEngine();
this.skinEngine.SkinFile = AppDomain.CurrentDomain.BaseDirectory + @"Skins\mp10.ssk";
this.skinEngine.DisableTag = ;
ReadSkinFile();
}

切换背景图

  /// <summary>
/// 背景图切换
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BgWorker1_DoWork(object sender, DoWorkEventArgs e)
{
while (isRunning) {
if (bgUrls != null && bgUrls.Length > ) {
this.curIndex = (this.curIndex % bgUrls.Length);
this.pbImage.Image = Image.FromStream(HttpWebRequest.Create(bgUrls[this.curIndex]).GetResponse().GetResponseStream());
this.pbImage.SizeMode = PictureBoxSizeMode.StretchImage;
this.curIndex++;
//
this.lblTime.Invoke(new Action(() =>
{
this.lblTime.Text = string.Format("{0}/{1}", this.axWindowsMediaPlayer1.Ctlcontrols.currentPositionString, this.axWindowsMediaPlayer1.currentMedia.durationString);
//this.lblTime.ForeColor = Color.White;
})); }
Thread.Sleep();
}
}

AxWindowsMediaPlayer相关属性方法

 [基本属性]  
URL:String; 指定媒体位置,本机或网络地址
uiMode:String; 播放器界面模式,可为Full, Mini, None, Invisible
playState:integer; 播放状态,=停止,=暂停,=播放,=正在缓冲,=正在连接,=准备就绪
enableContextMenu:Boolean; 启用/禁用右键菜单
fullScreen:boolean; 是否全屏显示
//播放器基本控制
Ctlcontrols.play; 播放
Ctlcontrols.pause; 暂停
Ctlcontrols.stop; 停止
Ctlcontrols.currentPosition:double; 当前进度
Ctlcontrols.currentPositionString:string; 当前进度,字符串格式。如“:”
Ctlcontrols.fastForward; 快进
Ctlcontrols.fastReverse; 快退
Ctlcontrols.next; 下一曲
Ctlcontrols.previous; 上一曲
[settings] wmp.settings //播放器基本设置
settings.volume:integer; 音量,-
settings.autoStart:Boolean; 是否自动播放
settings.mute:Boolean; 是否静音
settings.playCount:integer; 播放次数
[currentMedia] wmp.currentMedia //当前媒体属性
currentMedia.duration:double; 媒体总长度
currentMedia.durationString:string; 媒体总长度,字符串格式。如“:”
currentMedia.getItemInfo(const string); 获取当前媒体信息"Title"=媒体标题,"Author"=艺术家,"Copyright"=版权信息,"Description"=媒体内容描述, "Duration"=持续时间(秒),"FileSize"=文件大小,"FileType"=文件类型,"sourceURL"=原始地址
currentMedia.setItemInfo(const string); 通过属性名设置媒体信息
currentMedia.name:string; 同 currentMedia.getItemInfo("Title")
[currentPlaylist] wmp.currentPlaylist //当前播放列表属性
currentPlaylist.count:integer; 当前播放列表所包含媒体数
currentPlaylist.Item[integer]; 获取或设置指定项目媒体信息,其子属性同wmp.currentMedia
axWindowsMediaPlayer1.currentMedia.sourceURL; //获取正在播放的媒体文件的路径
axWindowsMediaPlayer1.currentMedia.name; //获取正在播放的媒体文件的名称
axWindowsMediaPlayer1.Ctlcontrols.Play          播放
axWindowsMediaPlayer1.Ctlcontrols.Stop          停止
axWindowsMediaPlayer1.Ctlcontrols.Pause          暂停
axWindowsMediaPlayer1.Ctlcontrols.PlayCount        文件播放次数
axWindowsMediaPlayer1.Ctlcontrols.AutoRewind       是否循环播放
axWindowsMediaPlayer1.Ctlcontrols.Balance         声道
axWindowsMediaPlayer1.Ctlcontrols.Volume         音量
axWindowsMediaPlayer1.Ctlcontrols.Mute          静音
axWindowsMediaPlayer1.Ctlcontrols.EnableContextMenu    是否允许在控件上点击鼠标右键时弹出快捷菜单
axWindowsMediaPlayer1.Ctlcontrols.AnimationAtStart    是否在播放前先播放动画
axWindowsMediaPlayer1.Ctlcontrols.ShowControls      是否显示控件工具栏
axWindowsMediaPlayer1.Ctlcontrols.ShowAudioControls    是否显示声音控制按钮
axWindowsMediaPlayer1.Ctlcontrols.ShowDisplay       是否显示数据文件的相关信息
axWindowsMediaPlayer1.Ctlcontrols.ShowGotoBar       是否显示Goto栏
axWindowsMediaPlayer1.Ctlcontrols.ShowPositionControls  是否显示位置调节按钮
axWindowsMediaPlayer1.Ctlcontrols.ShowStatusBar      是否显示状态栏
axWindowsMediaPlayer1.Ctlcontrols.ShowTracker       是否显示进度条
axWindowsMediaPlayer1.Ctlcontrols.FastForward       快进
axWindowsMediaPlayer1.Ctlcontrols.FastReverse       快退
axWindowsMediaPlayer1.Ctlcontrols.Rate          快进/快退速率
axWindowsMediaPlayer1.AllowChangeDisplaySize 是否允许*设置播放图象大小
axWindowsMediaPlayer1.DisplaySize       设置播放图象大小
    -MpDefaultSize         原始大小
    -MpHalfSize           原始大小的一半
    -MpDoubleSize          原始大小的两倍
    -MpFullScreen          全屏
    -MpOneSixteenthScreen      屏幕大小的1/
    -MpOneFourthScreen       屏幕大小的1/
    -MpOneHalfScreen        屏幕大小的1/
axWindowsMediaPlayer1.ClickToPlay       是否允许单击播放窗口启动Media Player

备注

关于AxWindowsMediaPlayer的属性方法,也可以参考豆丁文档。

源码下载链接